Skip to content

Commit

Permalink
Fix: missing Handled errors from ASP.NET Core. (#1111)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey Golub <tyrrrrrr@gmail.com>
Co-authored-by: Bruno Garcia <bruno@brunogarcia.com>
  • Loading branch information
3 people committed Jul 12, 2021
1 parent 5e21b5c commit 1b7e3c8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- ASP.NET Core: fix handled not being set for Handled exceptions ([#1111](https://github.com/getsentry/sentry-dotnet/pull/1111))

## 3.7.0

### Features
Expand Down
9 changes: 6 additions & 3 deletions src/Sentry.AspNetCore/SentryMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,21 @@ public async Task InvokeAsync(HttpContext context)
var exceptionFeature = context.Features.Get<IExceptionHandlerFeature?>();
if (exceptionFeature?.Error != null)
{
CaptureException(exceptionFeature.Error);
CaptureException(exceptionFeature.Error, "IExceptionHandlerFeature");
}
}
catch (Exception e)
{
CaptureException(e);
CaptureException(e, "SentryMiddleware.UnhandledException");

ExceptionDispatchInfo.Capture(e).Throw();
}

void CaptureException(Exception e)
void CaptureException(Exception e, string mechanism)
{
e.Data[Mechanism.HandledKey] = false;
e.Data[Mechanism.MechanismKey] = mechanism;

var evt = new SentryEvent(e);

_logger.LogTrace("Sending event '{SentryEvent}' to Sentry.", evt);
Expand Down
32 changes: 32 additions & 0 deletions test/Sentry.AspNetCore.Tests/SentryMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ public async Task InvokeAsync_ExceptionThrown_SameRethrown()
Assert.Same(expected, actual);
}

[Fact]
public async Task InvokeAsync_ExceptionThrown_HandledSetFalse()
{
var expected = new Exception("test");
_fixture.RequestDelegate = _ => throw expected;

_fixture.Hub.When(h => h.CaptureEvent(Arg.Any<SentryEvent>()))
.Do(c => Assert.False((bool)c.Arg<SentryEvent>().Exception.Data[Mechanism.HandledKey]));

var sut = _fixture.GetSut();

var actual = await Assert.ThrowsAsync<Exception>(
async () => await sut.InvokeAsync(_fixture.HttpContext));

Assert.Same(expected, actual);
}

[Fact]
public async Task InvokeAsync_FeatureFoundWithNoError_DoesNotCapturesEvent()
{
Expand All @@ -102,6 +119,21 @@ public async Task InvokeAsync_FeatureFoundWithNoError_DoesNotCapturesEvent()
_ = _fixture.Hub.DidNotReceive().CaptureEvent(Arg.Any<SentryEvent>());
}

[Fact]
public async Task InvokeAsync_FeatureFoundWithError_CapturesEvent()
{
var exception = new Exception();
var feature = Substitute.For<IExceptionHandlerFeature>();
_ = feature.Error.Returns(exception);
_ = _fixture.HttpContext.Features.Get<IExceptionHandlerFeature>().Returns(feature);
var sut = _fixture.GetSut();

await sut.InvokeAsync(_fixture.HttpContext);

_ = _fixture.Hub.Received().CaptureEvent(Arg.Any<SentryEvent>());
Assert.Equal("IExceptionHandlerFeature", exception.Data[Mechanism.MechanismKey]);
}

[Fact]
public async Task InvokeAsync_ScopePushed_BeforeConfiguringScope()
{
Expand Down

0 comments on commit 1b7e3c8

Please sign in to comment.