-
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CaptureLastError for ASP.NET (#1411)
Co-authored-by: Bruno Garcia <bruno@brunogarcia.com> Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
- Loading branch information
1 parent
e7de7db
commit e0224eb
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Web; | ||
using Sentry.Extensibility; | ||
using Sentry.Protocol; | ||
|
||
namespace Sentry.AspNet; | ||
|
||
/// <summary> | ||
/// HttpServerUtility extensions. | ||
/// </summary> | ||
public static class SentryHttpServerUtilityExtensions | ||
{ | ||
/// <summary> | ||
/// Captures the last error from the given HttpServerUtility and sends it to Sentry. | ||
/// </summary> | ||
/// <param name="server">The HttpServerUtility that contains the last error.</param> | ||
/// <returns>A SentryId.</returns> | ||
public static SentryId CaptureLastError(this HttpServerUtility server) => server.CaptureLastError(HubAdapter.Instance); | ||
|
||
// for testing | ||
internal static SentryId CaptureLastError(this HttpServerUtility server, IHub hub) | ||
{ | ||
if (server.GetLastError() is { } exception) | ||
{ | ||
exception.Data[Mechanism.HandledKey] = false; | ||
exception.Data[Mechanism.MechanismKey] = "HttpApplication.Application_Error"; | ||
return hub.CaptureException(exception); | ||
} | ||
return SentryId.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
test/Sentry.AspNet.Tests/SentryHttpServerUtilityExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Web; | ||
|
||
namespace Sentry.AspNet.Tests; | ||
|
||
public class SentryHttpServerUtilityExtensionsTests | ||
{ | ||
private class Fixture | ||
{ | ||
public SentryId Id { get; set; } | ||
|
||
public IHub GetSut() | ||
{ | ||
Id = SentryId.Create(); | ||
var hub = Substitute.For<IHub>(); | ||
hub.IsEnabled.Returns(true); | ||
hub.CaptureEvent(Arg.Any<SentryEvent>()).Returns(Id); | ||
return hub; | ||
} | ||
} | ||
|
||
private readonly Fixture _fixture = new(); | ||
|
||
[Fact] | ||
public void CaptureLastError_WithError_UnhandledErrorCaptured() | ||
{ | ||
// Arrange | ||
var hub = _fixture.GetSut(); | ||
var exception = new Exception(); | ||
|
||
var context = new HttpContext(new HttpRequest("", "http://test", null), new HttpResponse(new StringWriter())); | ||
context.AddError(exception); | ||
|
||
// Act | ||
var receivedId = context.Server.CaptureLastError(hub); | ||
|
||
// Assert | ||
hub.Received(1).CaptureEvent(Arg.Is<SentryEvent>(@event => @event.Exception == exception)); | ||
Assert.False(exception.Data[Mechanism.HandledKey] as bool?); | ||
Assert.Equal("HttpApplication.Application_Error", exception.Data[Mechanism.MechanismKey]); | ||
Assert.Equal(_fixture.Id, receivedId); | ||
} | ||
|
||
[Fact] | ||
public void CaptureLastError_WithoutError_DoNothing() | ||
{ | ||
// Arrange | ||
var hub = _fixture.GetSut(); | ||
var context = new HttpContext(new HttpRequest("", "http://test", null), new HttpResponse(new StringWriter())); | ||
|
||
// Act | ||
var receivedId = context.Server.CaptureLastError(hub); | ||
|
||
// Assert | ||
hub.Received(0).CaptureEvent(Arg.Any<SentryEvent>()); | ||
Assert.Equal(SentryId.Empty, receivedId); | ||
} | ||
} |