Skip to content

Commit

Permalink
Update functionality after PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
aholovko committed Mar 6, 2018
1 parent 4fb6c36 commit 758815d
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 86 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -96,7 +96,7 @@ namespace ConsoleApplication
}
catch (Exception ex)
{
var notice = airbrake.CreateNotice(ex);
var notice = airbrake.BuildNotice(ex);
var response = airbrake.NotifyAsync(notice).Result;
Console.WriteLine("Status: {0}, Id: {1}, Url: {2}", response.Status, response.Id, response.Url);
}
Expand Down Expand Up @@ -330,13 +330,13 @@ var config = AirbrakeConfig.Load(settings);

### AirbrakeNotifier

#### CreateNotice
#### BuildNotice

`CreateNotice` creates a notice with error details that can be sent to the
`BuildNotice` builds a notice with error details that can be sent to the
Airbrake dashboard with the help of [`NotifyAsync`](#notifyasync):

```csharp
notifier.CreateNotice(Severity.Error, exception, "Failed with message {0}", exception.Message);
notifier.BuildNotice(Severity.Error, exception, "Failed with message {0}", exception.Message);
```

#### NotifyAsync
Expand All @@ -363,7 +363,7 @@ Console.WriteLine(response.Url);
`SetHttpContext` attaches HTTP context properties to the notice:

```csharp
var notice = notifier.CreateNotice(ex);
var notice = notifier.BuildNotice(ex);
notifier.SetHttpContext(notice, new AspNetCoreHttpContext(context));
notifier.NotifyAsync(notice);
```
Expand Down Expand Up @@ -440,10 +440,10 @@ airbrake.AddFilter(notice =>

[Severity][what-is-severity] allows categorizing how severe an error is. By
default, it's set to `error`. To redefine severity, simply pass the `Severity`
as a parameter to the `CreateNotice` method. For example:
as a parameter to the `BuildNotice` method. For example:

```csharp
airbrake.CreateNotice(Severity.Critical, exception);
airbrake.BuildNotice(Severity.Critical, exception);
```

ASP.NET Integration
Expand Down
2 changes: 1 addition & 1 deletion docs/asp-net-core-middleware.md
Expand Up @@ -72,7 +72,7 @@ public IActionResult Contact()
if (airbrakeFeature != null)
{
var notifier = airbrakeFeature.GetNotifier();
var notice = notifier.CreateNotice(ex);
var notice = notifier.BuildNotice(ex);
notice.SetHttpContext(notice, new AspNetCoreHttpContext(HttpContext));
notifier.Notify(notice);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/asp-net-http-module.md
Expand Up @@ -98,7 +98,7 @@ public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
var airbrake = (AirbrakeHttpModule)HttpContext.ApplicationInstance.Modules["Airbrake"];
var notifier = airbrake.GetNotifier();
var notice = notifier.CreateNotice(ex);
var notice = notifier.BuildNotice(ex);
notice.SetHttpContext(notice, new AspNetHttpContext(System.Web.HttpContext.Current));
notifier.Notify(notice);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/dotnet-core-console.md
Expand Up @@ -90,7 +90,7 @@ If you see `dotnet: command not found`, then follow the steps described in https
}
catch (Exception ex)
{
var notice = airbrake.CreateNotice(ex);
var notice = airbrake.BuildNotice(ex);
var response = airbrake.NotifyAsync(notice).Result;
Console.WriteLine("Status: {0}, Id: {1}, Url: {2}", response.Status, response.Id, response.Url);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/ConsoleApp/Program.cs
Expand Up @@ -38,7 +38,7 @@ static void Case1(IDictionary<string, string> settings)
}
catch (Exception ex)
{
var notice = notifier.CreateNotice(ex);
var notice = notifier.BuildNotice(ex);
notifier.NotifyAsync(notice).ContinueWith(task =>
{
if (task.IsFaulted)
Expand Down Expand Up @@ -66,7 +66,7 @@ static async void Case2(IDictionary<string, string> settings)
}
catch (Exception ex)
{
var notice = notifier.CreateNotice(ex);
var notice = notifier.BuildNotice(ex);
var response = await notifier.NotifyAsync(notice);
if (response != null)
Console.WriteLine("Status: {0}, Id: {1}, Url: {2}", response.Status, response.Id, response.Url);
Expand Down
36 changes: 18 additions & 18 deletions src/Sharpbrake.Client/AirbrakeNotifier.cs
Expand Up @@ -60,67 +60,67 @@ public void AddFilter(Func<Notice, Notice> filter)
}

/// <summary>
/// Creates a notice for the exception with <see cref="Severity.Error"/> severity.
/// Builds a notice for the exception with <see cref="Severity.Error"/> severity.
/// </summary>
/// <param name="exception">Exception to report on.</param>
public Notice CreateNotice(Exception exception)
public Notice BuildNotice(Exception exception)
{
return CreateNotice(Severity.Error, exception, null, null);
return BuildNotice(Severity.Error, exception, null, null);
}

/// <summary>
/// Creates a notice for the error with <see cref="Severity.Error"/> severity.
/// Builds a notice for the error with <see cref="Severity.Error"/> severity.
/// </summary>
/// <param name="message">Message describing the error.</param>
/// <param name="args">Objects positionally formatted into the error message.</param>
public Notice CreateNotice(string message, params object[] args)
public Notice BuildNotice(string message, params object[] args)
{
return CreateNotice(Severity.Error, null, message, args);
return BuildNotice(Severity.Error, null, message, args);
}

/// <summary>
/// Creates a notice for the error with <see cref="Severity.Error"/> severity and associated exception.
/// Builds a notice for the error with <see cref="Severity.Error"/> severity and associated exception.
/// </summary>
/// <param name="exception">Exception associated with the error.</param>
/// <param name="message">Message describing the error.</param>
/// <param name="args">Objects positionally formatted into the error message.</param>
public Notice CreateNotice(Exception exception, string message, params object[] args)
public Notice BuildNotice(Exception exception, string message, params object[] args)
{
return CreateNotice(Severity.Error, exception, message, args);
return BuildNotice(Severity.Error, exception, message, args);
}

/// <summary>
/// Creates a notice for the exception with specified severity.
/// Builds a notice for the exception with specified severity.
/// </summary>
/// <param name="severity">Severity level of the error.</param>
/// <param name="exception">Exception to report on.</param>
public Notice CreateNotice(Severity severity, Exception exception)
public Notice BuildNotice(Severity severity, Exception exception)
{
return CreateNotice(severity, exception, null, null);
return BuildNotice(severity, exception, null, null);
}

/// <summary>
/// Creates a notice for the error with specified severity.
/// Builds a notice for the error with specified severity.
/// </summary>
/// <param name="severity">Severity level of the error.</param>
/// <param name="message">Message describing the error.</param>
/// <param name="args">Objects positionally formatted into the error message.</param>
public Notice CreateNotice(Severity severity, string message, params object[] args)
public Notice BuildNotice(Severity severity, string message, params object[] args)
{
return CreateNotice(severity, null, message, args);
return BuildNotice(severity, null, message, args);
}

/// <summary>
/// Creates a notice for the error with specified severity and associated exception.
/// Builds a notice for the error with specified severity and associated exception.
/// </summary>
/// <param name="severity">Severity level of the error.</param>
/// <param name="exception">Exception associated with the error.</param>
/// <param name="message">Message describing the error.</param>
/// <param name="args">Objects positionally formatted into the error message.</param>
public Notice CreateNotice(Severity severity, Exception exception, string message, params object[] args)
public Notice BuildNotice(Severity severity, Exception exception, string message, params object[] args)
{
var log = InternalLogger.CreateInstance();
var notice = NoticeBuilder.CreateNotice();
var notice = NoticeBuilder.BuildNotice();

log.Trace("Setting error entries");
notice.SetErrorEntries(exception,
Expand Down
6 changes: 4 additions & 2 deletions src/Sharpbrake.Client/NoticeBuilder.cs
Expand Up @@ -14,10 +14,12 @@ namespace Sharpbrake.Client
/// </summary>
public static class NoticeBuilder
{
private const int MaxInnerExceptions = 3;

/// <summary>
/// Creates a new instance of <see cref="Notice"/>.
/// </summary>
public static Notice CreateNotice()
public static Notice BuildNotice()
{
return new Notice
{
Expand Down Expand Up @@ -63,7 +65,7 @@ public static void SetErrorEntries(this Notice notice, Exception exception, stri
}

// to reduce JSON size no more than 3 inner exceptions are processed
while (ex != null && errors.Count < 4)
while (ex != null && errors.Count <= MaxInnerExceptions)
{
errors.Add(new ErrorEntry
{
Expand Down
2 changes: 1 addition & 1 deletion src/Sharpbrake.Extensions.Logging/AirbrakeLogger.cs
Expand Up @@ -39,7 +39,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
if (formatter != null)
message = formatter(state, exception);

var notice = notifier.CreateNotice(GetErrorSeverity(logLevel), exception, message);
var notice = notifier.BuildNotice(GetErrorSeverity(logLevel), exception, message);
notifier.SetHttpContext(notice, GetHttpContext());
notifier.NotifyAsync(notice);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sharpbrake.Http.Middleware/AirbrakeMiddleware.cs
Expand Up @@ -66,7 +66,7 @@ public async Task Invoke(HttpContext context)
}
catch (Exception ex)
{
var notice = notifier.CreateNotice(ex);
var notice = notifier.BuildNotice(ex);
notifier.SetHttpContext(notice, new AspNetCoreHttpContext(context));
await notifier.NotifyAsync(notice);
throw;
Expand Down
2 changes: 1 addition & 1 deletion src/Sharpbrake.Http.Module/AirbrakeHttpModule.cs
Expand Up @@ -40,7 +40,7 @@ public void Init(HttpApplication application)
var exception = app.Server.GetLastError().GetBaseException();
var context = new AspNetHttpContext(app.Context);
var notice = notifier.CreateNotice(exception);
var notice = notifier.BuildNotice(exception);
notifier.SetHttpContext(notice, context);
notifier.NotifyAsync(notice);
};
Expand Down
2 changes: 1 addition & 1 deletion src/Sharpbrake.Log4net/AirbrakeAppender.cs
Expand Up @@ -129,7 +129,7 @@ private static Severity GetErrorSeverity(Level logLevel)
/// </summary>
protected override void Append(LoggingEvent logEvent)
{
var notice = Notifier.CreateNotice(GetErrorSeverity(logEvent.Level), logEvent.ExceptionObject, logEvent.RenderedMessage);
var notice = Notifier.BuildNotice(GetErrorSeverity(logEvent.Level), logEvent.ExceptionObject, logEvent.RenderedMessage);
Notifier.SetHttpContext(notice, GetHttpContext());
Notifier.NotifyAsync(notice);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sharpbrake.NLog/AirbrakeTarget.cs
Expand Up @@ -137,7 +137,7 @@ protected override void InitializeTarget()
/// </summary>
protected override void Write(LogEventInfo logEvent)
{
var notice = Notifier.CreateNotice(GetErrorSeverity(logEvent.Level), logEvent.Exception, logEvent.FormattedMessage);
var notice = Notifier.BuildNotice(GetErrorSeverity(logEvent.Level), logEvent.Exception, logEvent.FormattedMessage);
Notifier.SetHttpContext(notice, GetHttpContext());
Notifier.NotifyAsync(notice);
}
Expand Down
38 changes: 19 additions & 19 deletions test/Sharpbrake.Client.Tests/AirbrakeNotifierTests.cs
Expand Up @@ -24,36 +24,36 @@ public void Ctor_ShouldThrowExceptionIfConfigIsNotProvided()
}

[Fact]
public void CreateNotice_ShouldSetDefaultSeverityToError()
public void BuildNotice_ShouldSetDefaultSeverityToError()
{
var notifier = new AirbrakeNotifier(new AirbrakeConfig());
var notice1 = notifier.CreateNotice("message");
var notice2 = notifier.CreateNotice(new Exception());
var notice1 = notifier.BuildNotice("message");
var notice2 = notifier.BuildNotice(new Exception());

Assert.Equal(Severity.Error.ToString().ToLowerInvariant(), notice1.Context.Severity);
Assert.Equal(Severity.Error.ToString().ToLowerInvariant(), notice2.Context.Severity);
}

[Fact]
public void CreateNotice_ShouldSetErrorEntriesIfExceptionProvided()
public void BuildNotice_ShouldSetErrorEntriesIfExceptionProvided()
{
var notifier = new AirbrakeNotifier(new AirbrakeConfig());
var notice = notifier.CreateNotice(Severity.Error, new Exception());
var notice = notifier.BuildNotice(Severity.Error, new Exception());

Assert.NotNull(notice.Errors);
}

[Fact]
public void CreateNotice_ShouldSetErrorEntriesIfMessageProvided()
public void BuildNotice_ShouldSetErrorEntriesIfMessageProvided()
{
var notifier = new AirbrakeNotifier(new AirbrakeConfig());
var notice = notifier.CreateNotice(Severity.Error, "message {0}", 1);
var notice = notifier.BuildNotice(Severity.Error, "message {0}", 1);

Assert.NotNull(notice.Errors);
}

[Fact]
public void CreateNotice_ShouldSetConfigurationContext()
public void BuildNotice_ShouldSetConfigurationContext()
{
var config = new AirbrakeConfig
{
Expand All @@ -64,18 +64,18 @@ public void CreateNotice_ShouldSetConfigurationContext()
};

var notifier = new AirbrakeNotifier(config);
var notice = notifier.CreateNotice(Severity.Error, "message");
var notice = notifier.BuildNotice(Severity.Error, "message");

Assert.NotNull(notice.Context);
Assert.NotNull(notice.Context.EnvironmentName);
Assert.NotNull(notice.Context.AppVersion);
}

[Fact]
public void CreateNotice_ShouldSetEnvironmentContext()
public void BuildNotice_ShouldSetEnvironmentContext()
{
var notifier = new AirbrakeNotifier(new AirbrakeConfig());
var notice = notifier.CreateNotice(Severity.Error, new Exception(), "message");
var notice = notifier.BuildNotice(Severity.Error, new Exception(), "message");

Assert.NotNull(notice.Context);
Assert.NotNull(notice.Context.Hostname);
Expand All @@ -87,7 +87,7 @@ public void CreateNotice_ShouldSetEnvironmentContext()
public void SetHttpContext_ShouldSetHttpContext()
{
var notifier = new AirbrakeNotifier(new AirbrakeConfig());
var notice = NoticeBuilder.CreateNotice();
var notice = NoticeBuilder.BuildNotice();
var context = new FakeHttpContext();

notifier.SetHttpContext(notice, context);
Expand All @@ -109,7 +109,7 @@ public void NotifyAsync_ShouldThrowExceptionIfProjectIdOrKeyIsNotSet(string proj
using (var requestHandler = new FakeHttpRequestHandler())
{
var notifier = new AirbrakeNotifier(config, requestHandler);
var exceptionTask = Record.ExceptionAsync(() => Task.Run(() => notifier.NotifyAsync(NoticeBuilder.CreateNotice())));
var exceptionTask = Record.ExceptionAsync(() => Task.Run(() => notifier.NotifyAsync(NoticeBuilder.BuildNotice())));

Assert.NotNull(exceptionTask);

Expand All @@ -134,7 +134,7 @@ public void NotifyAsync_ShouldSetStatusToIgnoredIfEnvironmentIsIgnored()
using (var requestHandler = new FakeHttpRequestHandler())
{
var notifier = new AirbrakeNotifier(config, requestHandler);
var response = notifier.NotifyAsync(NoticeBuilder.CreateNotice()).Result;
var response = notifier.NotifyAsync(NoticeBuilder.BuildNotice()).Result;

Assert.True(response.Status == RequestStatus.Ignored);
}
Expand Down Expand Up @@ -180,7 +180,7 @@ public void NotifyAsync_ShouldUpdateNoticeAfterApplyingFilters()
return n;
});

var notice = notifier.CreateNotice(new Exception());
var notice = notifier.BuildNotice(new Exception());
var response = notifier.NotifyAsync(notice).Result;
var actualNotice = NoticeBuilder.FromJsonString(requestHandler.HttpRequest.GetRequestStreamContent());

Expand All @@ -204,7 +204,7 @@ public void NotifyAsync_ShouldSetStatusToIgnoredIfNoticeIsNullAfterApplyingFilte
var notifier = new AirbrakeNotifier(config, requestHandler);
notifier.AddFilter(n => null);

var notice = notifier.CreateNotice(new Exception());
var notice = notifier.BuildNotice(new Exception());
var response = notifier.NotifyAsync(notice).Result;

Assert.True(response.Status == RequestStatus.Ignored);
Expand All @@ -231,7 +231,7 @@ public void NotifyAsync_ShouldSetExceptionIfRequestStreamOrResponseIsFaulted(str
requestHandler.HttpRequest.IsFaultedGetResponse = faultedTask == "GetResponse";

var notifier = new AirbrakeNotifier(config, requestHandler);
var notifyTask = notifier.NotifyAsync(NoticeBuilder.CreateNotice());
var notifyTask = notifier.NotifyAsync(NoticeBuilder.BuildNotice());
var exceptionTask = Record.ExceptionAsync(() => notifyTask);

Assert.NotNull(exceptionTask);
Expand Down Expand Up @@ -263,7 +263,7 @@ public void NotifyAsync_ShouldSetCanceledIfRequestStreamOrResponseIsCanceled(str
requestHandler.HttpRequest.IsCanceledGetResponse = canceledTask == "GetResponse";

var notifier = new AirbrakeNotifier(config, requestHandler);
var notifyTask = notifier.NotifyAsync(NoticeBuilder.CreateNotice());
var notifyTask = notifier.NotifyAsync(NoticeBuilder.BuildNotice());
var exceptionTask = Record.ExceptionAsync(() => notifyTask);

Assert.NotNull(exceptionTask);
Expand Down Expand Up @@ -294,7 +294,7 @@ public void NotifyAsync_ShouldSetRequestStatusToSuccessOnlyIfStatusCodeCreated(b
requestHandler.HttpResponse.ResponseJson = "{\"Id\":\"12345\",\"Url\":\"https://airbrake.io/\"}";

var notifier = new AirbrakeNotifier(config, requestHandler);
var airbrakeResponse = notifier.NotifyAsync(NoticeBuilder.CreateNotice()).Result;
var airbrakeResponse = notifier.NotifyAsync(NoticeBuilder.BuildNotice()).Result;

if (isStatusCodeCreated)
Assert.True(airbrakeResponse.Status == RequestStatus.Success);
Expand Down
2 changes: 1 addition & 1 deletion test/Sharpbrake.Client.Tests/InternalLoggerTests.cs
Expand Up @@ -53,7 +53,7 @@ private void NotifyAsync()
requestHandler.HttpResponse.ResponseJson = "{\"Id\":\"12345\",\"Url\":\"https://airbrake.io/\"}";

var notifier = new AirbrakeNotifier(config, requestHandler);
var notice = notifier.CreateNotice(new Exception());
var notice = notifier.BuildNotice(new Exception());
var airbrakeResponse = notifier.NotifyAsync(notice).Result;
}
}
Expand Down

0 comments on commit 758815d

Please sign in to comment.