Skip to content

Commit 424ac02

Browse files
authored
Ensure mechanism data is not fully overwritten (#4106)
1 parent 3f9dd96 commit 424ac02

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- Reintroduced experimental support for Session Replay on Android ([#4097](https://github.com/getsentry/sentry-dotnet/pull/4097))
88

99
### Fixes
10-
10+
- Ensure user exception data is not removed by AspNetCoreExceptionProcessor ([#4016](https://github.com/getsentry/sentry-dotnet/pull/4106))
1111
- Prevent users from disabling AndroidEnableAssemblyCompression which leads to untrappable crash ([#4089](https://github.com/getsentry/sentry-dotnet/pull/4089))
1212
- Fixed MSVCRT build warning on Windows ([#4111](https://github.com/getsentry/sentry-dotnet/pull/4111))
1313

src/Sentry.AspNetCore/AspNetCoreExceptionProcessor.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ public void Process(Exception exception, SentryEvent @event)
1414
{
1515
foreach (var ex in @event.SentryExceptions)
1616
{
17-
ex.Mechanism = new Mechanism
18-
{
19-
Type = "ExceptionHandlerMiddleware",
20-
Handled = false
21-
};
17+
ex.Mechanism ??= new Mechanism();
18+
ex.Mechanism.Type = "ExceptionHandlerMiddleware";
19+
ex.Mechanism.Handled = false;
2220
}
2321
}
2422
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace Sentry.AspNetCore.Tests;
2+
3+
public class AspNetCoreExceptionProcessorTests
4+
{
5+
private readonly AspNetCoreExceptionProcessor _sut = new();
6+
7+
[Fact]
8+
public void Process_Event()
9+
{
10+
var @event = new SentryEvent();
11+
@event.Logger = "Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware";
12+
13+
_sut.Process(null!, @event);
14+
15+
foreach (var ex in @event.SentryExceptions)
16+
{
17+
ex.Mechanism.Should().NotBeNull();
18+
ex.Mechanism.Type.Should().Be("ExceptionHandlerMiddleware");
19+
ex.Mechanism.Handled.Should().BeFalse();
20+
}
21+
}
22+
23+
24+
[Fact]
25+
public void Process_ShouldNotOverwriteMechanism()
26+
{
27+
var @event = new SentryEvent();
28+
var mechanism = new Mechanism();
29+
mechanism.Data.Add("key", "value");
30+
31+
@event.SentryExceptions = [new SentryException
32+
{
33+
Mechanism = mechanism
34+
}];
35+
@event.Logger = "Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware";
36+
37+
_sut.Process(null!, @event);
38+
39+
var mech = @event.SentryExceptions.First().Mechanism;
40+
mech.Should().NotBeNull();
41+
mech.Data.First().Key.Should().Be("key");
42+
mech.Data.First().Value.Should().Be("value");
43+
}
44+
}

0 commit comments

Comments
 (0)