Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: missing Handled errors from ASP.NET Core. #1111

Merged
merged 7 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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