diff --git a/CHANGELOG.md b/CHANGELOG.md index cfe074f126..ed7a9ac664 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ - Reintroduced experimental support for Session Replay on Android ([#4097](https://github.com/getsentry/sentry-dotnet/pull/4097)) ### Fixes - +- Ensure user exception data is not removed by AspNetCoreExceptionProcessor ([#4016](https://github.com/getsentry/sentry-dotnet/pull/4106)) - Prevent users from disabling AndroidEnableAssemblyCompression which leads to untrappable crash ([#4089](https://github.com/getsentry/sentry-dotnet/pull/4089)) - Fixed MSVCRT build warning on Windows ([#4111](https://github.com/getsentry/sentry-dotnet/pull/4111)) diff --git a/src/Sentry.AspNetCore/AspNetCoreExceptionProcessor.cs b/src/Sentry.AspNetCore/AspNetCoreExceptionProcessor.cs index c31d24570f..ace6284570 100644 --- a/src/Sentry.AspNetCore/AspNetCoreExceptionProcessor.cs +++ b/src/Sentry.AspNetCore/AspNetCoreExceptionProcessor.cs @@ -14,11 +14,9 @@ public void Process(Exception exception, SentryEvent @event) { foreach (var ex in @event.SentryExceptions) { - ex.Mechanism = new Mechanism - { - Type = "ExceptionHandlerMiddleware", - Handled = false - }; + ex.Mechanism ??= new Mechanism(); + ex.Mechanism.Type = "ExceptionHandlerMiddleware"; + ex.Mechanism.Handled = false; } } } diff --git a/test/Sentry.AspNetCore.Tests/AspNetCoreExceptionProcessorTests.cs b/test/Sentry.AspNetCore.Tests/AspNetCoreExceptionProcessorTests.cs new file mode 100644 index 0000000000..ff32a42d74 --- /dev/null +++ b/test/Sentry.AspNetCore.Tests/AspNetCoreExceptionProcessorTests.cs @@ -0,0 +1,44 @@ +namespace Sentry.AspNetCore.Tests; + +public class AspNetCoreExceptionProcessorTests +{ + private readonly AspNetCoreExceptionProcessor _sut = new(); + + [Fact] + public void Process_Event() + { + var @event = new SentryEvent(); + @event.Logger = "Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware"; + + _sut.Process(null!, @event); + + foreach (var ex in @event.SentryExceptions) + { + ex.Mechanism.Should().NotBeNull(); + ex.Mechanism.Type.Should().Be("ExceptionHandlerMiddleware"); + ex.Mechanism.Handled.Should().BeFalse(); + } + } + + + [Fact] + public void Process_ShouldNotOverwriteMechanism() + { + var @event = new SentryEvent(); + var mechanism = new Mechanism(); + mechanism.Data.Add("key", "value"); + + @event.SentryExceptions = [new SentryException + { + Mechanism = mechanism + }]; + @event.Logger = "Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware"; + + _sut.Process(null!, @event); + + var mech = @event.SentryExceptions.First().Mechanism; + mech.Should().NotBeNull(); + mech.Data.First().Key.Should().Be("key"); + mech.Data.First().Value.Should().Be("value"); + } +}