Skip to content

Commit 04d8889

Browse files
committed
Wrap exception type with enum
1 parent 0c47b3f commit 04d8889

File tree

4 files changed

+73
-82
lines changed

4 files changed

+73
-82
lines changed

src/Sentry/Internal/Hub.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,8 @@ private SentryId CaptureEvent(SentryEvent evt, SentryHint? hint, Scope scope)
573573
scope.LastEventId = id;
574574
scope.SessionUpdate = null;
575575

576-
if (evt.HasTerminalException() && scope.Transaction is { } transaction)
576+
if (evt.GetExceptionType() is SentryEvent.ExceptionType.Unhandled
577+
&& scope.Transaction is { } transaction)
577578
{
578579
// Event contains a terminal exception -> finish any current transaction as aborted
579580
// Do this *after* the event was captured, so that the event is still linked to the transaction.

src/Sentry/SentryClient.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -347,22 +347,23 @@ private SentryId DoSendEvent(SentryEvent @event, SentryHint? hint, Scope? scope)
347347
return SentryId.Empty; // Dropped by BeforeSend callback
348348
}
349349

350-
if (processedEvent.HasUnhandledNonTerminalException())
351-
{
352-
_options.LogDebug("Ending session as 'Unhandled', due to non-terminal unhandled exception.");
353-
scope.SessionUpdate = _sessionManager.EndSession(SessionEndStatus.Unhandled);
354-
}
355-
else if (processedEvent.HasUnhandledException())
356-
{
357-
_options.LogDebug("Ending session as 'Crashed', due to terminal unhandled exception.");
358-
scope.SessionUpdate = _sessionManager.EndSession(SessionEndStatus.Crashed);
359-
}
360-
else if (processedEvent.HasException())
361-
{
362-
// Event contains a handled exception -> Report error
363-
// (this might return null if the session has already reported errors before)
364-
_options.LogDebug("Updating session by reporting an error.");
365-
scope.SessionUpdate = _sessionManager.ReportError();
350+
var exceptionType = processedEvent.GetExceptionType();
351+
switch (exceptionType)
352+
{
353+
case SentryEvent.ExceptionType.UnhandledNonTerminal:
354+
_options.LogDebug("Ending session as 'Unhandled', due to non-terminal unhandled exception.");
355+
scope.SessionUpdate = _sessionManager.EndSession(SessionEndStatus.Unhandled);
356+
break;
357+
358+
case SentryEvent.ExceptionType.Unhandled:
359+
_options.LogDebug("Ending session as 'Crashed', due to terminal unhandled exception.");
360+
scope.SessionUpdate = _sessionManager.EndSession(SessionEndStatus.Crashed);
361+
break;
362+
363+
case SentryEvent.ExceptionType.Handled:
364+
_options.LogDebug("Updating session by reporting an error.");
365+
scope.SessionUpdate = _sessionManager.ReportError();
366+
break;
366367
}
367368

368369
if (_options.SampleRate != null)

src/Sentry/SentryEvent.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,37 @@ public IReadOnlyList<string> Fingerprint
178178
/// <inheritdoc />
179179
public IReadOnlyDictionary<string, string> Tags => _tags ??= new Dictionary<string, string>();
180180

181-
internal bool HasException() => Exception is not null || SentryExceptions?.Any() == true;
181+
internal enum ExceptionType
182+
{
183+
None,
184+
Handled,
185+
Unhandled,
186+
UnhandledNonTerminal
187+
}
188+
189+
internal ExceptionType GetExceptionType()
190+
{
191+
if (!HasException())
192+
{
193+
return ExceptionType.None;
194+
}
195+
196+
if (HasUnhandledNonTerminalException())
197+
{
198+
return ExceptionType.UnhandledNonTerminal;
199+
}
200+
201+
if (HasUnhandledException())
202+
{
203+
return ExceptionType.Unhandled;
204+
}
182205

183-
internal bool HasUnhandledException()
206+
return ExceptionType.Handled;
207+
}
208+
209+
private bool HasException() => Exception is not null || SentryExceptions?.Any() == true;
210+
211+
private bool HasUnhandledException()
184212
{
185213
if (Exception?.Data[Mechanism.HandledKey] is false)
186214
{
@@ -190,7 +218,7 @@ internal bool HasUnhandledException()
190218
return SentryExceptions?.Any(e => e.Mechanism is { Handled: false }) ?? false;
191219
}
192220

193-
internal bool HasUnhandledNonTerminalException()
221+
private bool HasUnhandledNonTerminalException()
194222
{
195223
// Generally, an unhandled exception is considered terminal.
196224
// Exception: If it is an unhandled exception but the terminal flag is explicitly set to false.
@@ -213,8 +241,6 @@ terminal is false
213241
) ?? false;
214242
}
215243

216-
internal bool HasTerminalException() => HasUnhandledException() && !HasUnhandledNonTerminalException();
217-
218244
internal DynamicSamplingContext? DynamicSamplingContext { get; set; }
219245

220246
/// <summary>

test/Sentry.Tests/Protocol/SentryEventTests.cs

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -169,114 +169,77 @@ public void Redact_Redacts_Urls()
169169
}
170170

171171
[Fact]
172-
public void HasUnhandledException_WithUnhandledException_ReturnsTrue()
172+
public void GetExceptionType_NoException_ReturnsNone()
173173
{
174-
var exception = new Exception("test");
175-
exception.SetSentryMechanism("test", handled: false);
176-
var evt = new SentryEvent(exception);
174+
var evt = new SentryEvent();
177175

178-
Assert.True(evt.HasUnhandledException());
176+
Assert.Equal(SentryEvent.ExceptionType.None, evt.GetExceptionType());
179177
}
180178

181179
[Fact]
182-
public void HasUnhandledException_WithHandledException_ReturnsFalse()
180+
public void GetExceptionType_HandledException_ReturnsHandled()
183181
{
184182
var exception = new Exception("test");
185183
exception.SetSentryMechanism("test", handled: true);
186184
var evt = new SentryEvent(exception);
187185

188-
Assert.False(evt.HasUnhandledException());
189-
}
190-
191-
[Fact]
192-
public void HasUnhandledException_WithSentryExceptions_Unhandled_ReturnsTrue()
193-
{
194-
var evt = new SentryEvent
195-
{
196-
SentryExceptions = [new SentryException { Mechanism = new Mechanism { Handled = false } }]
197-
};
198-
199-
Assert.True(evt.HasUnhandledException());
186+
Assert.Equal(SentryEvent.ExceptionType.Handled, evt.GetExceptionType());
200187
}
201188

202189
[Fact]
203-
public void HasUnhandledException_WithSentryExceptions_Handled_ReturnsFalse()
190+
public void GetExceptionType_HandledExceptionViaSentryExceptions_ReturnsHandled()
204191
{
205192
var evt = new SentryEvent
206193
{
207194
SentryExceptions = [new SentryException { Mechanism = new Mechanism { Handled = true } }]
208195
};
209196

210-
Assert.False(evt.HasUnhandledException());
211-
}
212-
213-
[Fact]
214-
public void HasUnhandledNonTerminalException_WithNonTerminalMechanism_ReturnsTrue()
215-
{
216-
var exception = new Exception("test");
217-
exception.SetSentryMechanism("UnobservedTaskException", handled: false, terminal: false);
218-
var evt = new SentryEvent(exception);
219-
220-
Assert.True(evt.HasUnhandledNonTerminalException());
197+
Assert.Equal(SentryEvent.ExceptionType.Handled, evt.GetExceptionType());
221198
}
222199

223200
[Fact]
224-
public void HasUnhandledNonTerminalException_WithHandledException_ReturnsFalse()
201+
public void GetExceptionType_UnhandledTerminalException_ReturnsUnhandled()
225202
{
226203
var exception = new Exception("test");
227-
exception.SetSentryMechanism("UnobservedTaskException", handled: true);
204+
exception.SetSentryMechanism("AppDomain.UnhandledException", handled: false, terminal: true);
228205
var evt = new SentryEvent(exception);
229206

230-
Assert.False(evt.HasUnhandledNonTerminalException());
207+
Assert.Equal(SentryEvent.ExceptionType.Unhandled, evt.GetExceptionType());
231208
}
232209

233210
[Fact]
234-
public void HasUnhandledNonTerminalException_WithNullTerminal_ReturnsFalse()
211+
public void GetExceptionType_UnhandledTerminalExceptionViaSentryExceptions_ReturnsUnhandled()
235212
{
236-
// Terminal = null means default behavior (terminal)
213+
// Terminal = null or true means default behavior (terminal)
237214
var evt = new SentryEvent
238215
{
239216
SentryExceptions = [new SentryException { Mechanism = new Mechanism { Handled = false } }]
240217
};
241218

242-
Assert.False(evt.HasUnhandledNonTerminalException());
243-
}
244-
245-
[Fact]
246-
public void HasTerminalException_WithTerminalUnhandledException_ReturnsTrue()
247-
{
248-
var exception = new Exception("test");
249-
exception.SetSentryMechanism("AppDomain.UnhandledException", handled: false, terminal: true);
250-
var evt = new SentryEvent(exception);
251-
252-
Assert.True(evt.HasTerminalException());
219+
Assert.Equal(SentryEvent.ExceptionType.Unhandled, evt.GetExceptionType());
253220
}
254221

255222
[Fact]
256-
public void HasTerminalException_WithNonTerminalException_ReturnsFalse()
223+
public void GetExceptionType_UnhandledNonTerminalException_ReturnsUnhandledNonTerminal()
257224
{
258225
var exception = new Exception("test");
259226
exception.SetSentryMechanism("UnobservedTaskException", handled: false, terminal: false);
260227
var evt = new SentryEvent(exception);
261228

262-
Assert.False(evt.HasTerminalException());
229+
Assert.Equal(SentryEvent.ExceptionType.UnhandledNonTerminal, evt.GetExceptionType());
263230
}
264231

265232
[Fact]
266-
public void HasTerminalException_WithHandledException_ReturnsFalse()
233+
public void GetExceptionType_UnhandledNonTerminalExceptionViaSentryExceptions_ReturnsUnhandledNonTerminal()
267234
{
268-
var exception = new Exception("test");
269-
exception.SetSentryMechanism("test", handled: true);
270-
var evt = new SentryEvent(exception);
235+
var mechanism = new Mechanism { Handled = false };
236+
mechanism.Data[Mechanism.TerminalKey] = false;
271237

272-
Assert.False(evt.HasTerminalException());
273-
}
274-
275-
[Fact]
276-
public void HasTerminalException_NoException_ReturnsFalse()
277-
{
278-
var evt = new SentryEvent();
238+
var evt = new SentryEvent
239+
{
240+
SentryExceptions = [new SentryException { Mechanism = mechanism }]
241+
};
279242

280-
Assert.False(evt.HasTerminalException());
243+
Assert.Equal(SentryEvent.ExceptionType.UnhandledNonTerminal, evt.GetExceptionType());
281244
}
282245
}

0 commit comments

Comments
 (0)