Skip to content

Commit 664b37f

Browse files
Added device tests for the Android BeforeSendWrapper
1 parent 19edfc5 commit 664b37f

File tree

2 files changed

+109
-13
lines changed

2 files changed

+109
-13
lines changed

src/Sentry/Platforms/Android/SentrySdk.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,7 @@ private static void InitSentryAndroidSdk(SentryOptions options)
100100
}
101101
}
102102

103-
SentryEvent? BeforeSendWrapper(SentryEvent evt, SentryHint hint)
104-
{
105-
if (evt.SentryExceptions?.SingleOrDefault() is { Type: "SIGSEGV", Value: "Segfault" } exception)
106-
{
107-
options.LogDebug("Suppressing SIGSEGV (this will be thrown as a managed exception instead)");
108-
return null;
109-
}
110-
// Call the user defined BeforeSend callback, if it's defined - otherwise return the event as-is
111-
return (options.Native.EnableBeforeSend && options.BeforeSendInternal is { } beforeSend)
112-
? beforeSend(evt, hint)
113-
: evt;
114-
}
115-
o.BeforeSend = new BeforeSendCallback(BeforeSendWrapper, options, o);
103+
o.BeforeSend = new BeforeSendCallback(BeforeSendWrapper(options), options, o);
116104

117105
// These options are from SentryAndroidOptions
118106
o.AttachScreenshot = options.Native.AttachScreenshot;
@@ -182,6 +170,25 @@ private static void InitSentryAndroidSdk(SentryOptions options)
182170
// TODO: Pause/Resume
183171
}
184172

173+
internal static Func<SentryEvent, SentryHint, SentryEvent?> BeforeSendWrapper(SentryOptions options)
174+
{
175+
return (evt, hint) =>
176+
{
177+
// Suppress SIGSEGV errors.
178+
// See: https://github.com/getsentry/sentry-dotnet/pull/3903
179+
if (evt.SentryExceptions?.SingleOrDefault() is { Type: "SIGSEGV", Value: "Segfault" } exception)
180+
{
181+
options.LogDebug("Suppressing SIGSEGV (this will be thrown as a managed exception instead)");
182+
return null;
183+
}
184+
185+
// Call the user defined BeforeSend callback, if it's defined - otherwise return the event as-is
186+
return (options.Native.EnableBeforeSend && options.BeforeSendInternal is { } beforeSend)
187+
? beforeSend(evt, hint)
188+
: evt;
189+
};
190+
}
191+
185192
private static void AndroidEnvironment_UnhandledExceptionRaiser(object? _, RaiseThrowableEventArgs e)
186193
{
187194
var description = "This exception was caught by the Android global error handler.";
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#if ANDROID
2+
namespace Sentry.Tests.Platforms.Android;
3+
4+
public class SentrySdkTests
5+
{
6+
[Fact]
7+
public void BeforeSendWrapper_SuppressSIGSEGV_ReturnsNull()
8+
{
9+
// Arrange
10+
var options = new SentryOptions();
11+
var evt = new SentryEvent
12+
{
13+
SentryExceptions = new[]
14+
{
15+
new SentryException
16+
{
17+
Type = "SIGSEGV",
18+
Value = "Segfault"
19+
}
20+
}
21+
};
22+
var hint = new SentryHint();
23+
24+
// Act
25+
var result = SentrySdk.BeforeSendWrapper(options).Invoke(evt, hint);
26+
27+
// Assert
28+
result.Should().BeNull();
29+
}
30+
31+
[Fact]
32+
public void BeforeSendWrapper_BeforeSendCallbackDefined_CallsBeforeSend()
33+
{
34+
// Arrange
35+
var beforeSend = Substitute.For<Func<SentryEvent, SentryHint, SentryEvent>>();
36+
beforeSend.Invoke(Arg.Any<SentryEvent>(), Arg.Any<SentryHint>()).Returns(callInfo => callInfo.Arg<SentryEvent>());
37+
38+
var options = new SentryOptions();
39+
options.Native.EnableBeforeSend = true;
40+
options.SetBeforeSend(beforeSend);
41+
var evt = new SentryEvent();
42+
var hint = new SentryHint();
43+
44+
// Act
45+
var result = SentrySdk.BeforeSendWrapper(options).Invoke(evt, hint);
46+
47+
// Assert
48+
beforeSend.Received(1).Invoke(Arg.Any<SentryEvent>(), Arg.Any<SentryHint>());
49+
result.Should().Be(evt);
50+
}
51+
52+
[Fact]
53+
public void BeforeSendWrapper_NoBeforeSendCallback_ReturnsEvent()
54+
{
55+
// Arrange
56+
var options = new SentryOptions();
57+
options.Native.EnableBeforeSend = true;
58+
var evt = new SentryEvent();
59+
var hint = new SentryHint();
60+
61+
// Act
62+
var result = SentrySdk.BeforeSendWrapper(options).Invoke(evt, hint);
63+
64+
// Assert
65+
result.Should().Be(evt);
66+
}
67+
68+
[Fact]
69+
public void BeforeSendWrapper_NativeBeforeSendDisabled_ReturnsEvent()
70+
{
71+
// Arrange
72+
var beforeSend = Substitute.For<Func<SentryEvent, SentryHint, SentryEvent>>();
73+
beforeSend.Invoke(Arg.Any<SentryEvent>(), Arg.Any<SentryHint>()).Returns(_ => null);
74+
75+
var options = new SentryOptions();
76+
options.SetBeforeSend(beforeSend);
77+
options.Native.EnableBeforeSend = false;
78+
var evt = new SentryEvent();
79+
var hint = new SentryHint();
80+
81+
// Act
82+
var result = SentrySdk.BeforeSendWrapper(options).Invoke(evt, hint);
83+
84+
// Assert
85+
beforeSend.DidNotReceive().Invoke(Arg.Any<SentryEvent>(), Arg.Any<SentryHint>());
86+
result.Should().Be(evt);
87+
}
88+
}
89+
#endif

0 commit comments

Comments
 (0)