-
-
Notifications
You must be signed in to change notification settings - Fork 231
Add public API for checking unhandled exceptions #5177
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
Draft
jamescrosswell
wants to merge
6
commits into
main
Choose a base branch
from
fix/issue-2877-unhandled-exception-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
18e7843
Add public extension methods for checking unhandled exceptions
jamescrosswell ad784d8
Verify tests
jamescrosswell 220411f
windows verify
jamescrosswell 8fc8368
Remove redundant private methods in SentryEvent, delegate to extensio…
jamescrosswell b984ba2
Restore HasUnhandledNonTerminalException(), fix semantic bug
jamescrosswell d085338
Merge remote-tracking branch 'origin/main' into fix/issue-2877-unhand…
jamescrosswell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| using Sentry.Protocol; | ||
|
|
||
| namespace Sentry; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for <see cref="SentryEvent"/>. | ||
| /// </summary> | ||
| public static class SentryEventExtensions | ||
| { | ||
| /// <summary> | ||
| /// Determines whether this event was created from an unhandled exception. | ||
| /// </summary> | ||
| /// <param name="event">The Sentry event.</param> | ||
| /// <returns> | ||
| /// <c>true</c> if the event was created from an unhandled exception; otherwise, <c>false</c>. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// An unhandled exception is one that was not caught by application code and was instead | ||
| /// captured by the Sentry SDK through hooks like UnhandledExceptionHandler, ASP.NET Core | ||
| /// middleware, or other integration points. By default, Sentry marks exceptions as handled | ||
| /// unless explicitly captured through one of these unhandled exception hooks. | ||
| /// | ||
| /// This is useful for filtering events in callbacks like BeforeSend, where you may want to | ||
| /// treat unhandled exceptions differently from handled ones (e.g., always send unhandled | ||
| /// exceptions even if they match a filter that would normally exclude them). | ||
| /// </remarks> | ||
| /// <example> | ||
| /// <code> | ||
| /// options.SetBeforeSend((@event, hint) => | ||
| /// { | ||
| /// // Always send unhandled exceptions, even if they're network timeouts | ||
| /// if (@event.IsFromUnhandledException()) | ||
| /// { | ||
| /// return @event; | ||
| /// } | ||
| /// | ||
| /// // Filter out handled network timeout exceptions | ||
| /// if (@event.Exception is HttpRequestException) | ||
| /// { | ||
| /// return null; | ||
| /// } | ||
| /// | ||
| /// return @event; | ||
| /// }); | ||
| /// </code> | ||
| /// </example> | ||
| public static bool IsFromUnhandledException(this SentryEvent @event) | ||
| { | ||
| // Check if the original exception was marked as unhandled | ||
| if (@event.Exception?.Data[Mechanism.HandledKey] is false) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // Check if any of the Sentry exceptions have an unhandled mechanism | ||
| return @event.SentryExceptions?.Any(e => e.Mechanism is { Handled: false }) ?? false; | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// Determines whether this event was created from a terminal exception. | ||
| /// </summary> | ||
| /// <param name="event">The Sentry event.</param> | ||
| /// <returns> | ||
| /// <c>true</c> if the event was created from a terminal exception; otherwise, <c>false</c>. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// A terminal exception is an unhandled exception that caused the application to crash or | ||
| /// terminate. This excludes unhandled exceptions that were explicitly marked as non-terminal, | ||
| /// such as those captured by UnobservedTaskException handlers or certain Unity SDK integrations. | ||
| /// | ||
| /// In most cases, an unhandled exception is terminal. However, some integrations may capture | ||
| /// unhandled exceptions that don't actually crash the app (e.g., unobserved task exceptions) | ||
| /// and mark them with Terminal = false. | ||
| /// </remarks> | ||
| /// <example> | ||
| /// <code> | ||
| /// options.SetBeforeSend((@event, hint) => | ||
| /// { | ||
| /// // Only send terminal exceptions for certain exception types | ||
| /// if (@event.Exception is NetworkException && !@event.IsFromTerminalException()) | ||
| /// { | ||
| /// return null; // Don't send non-terminal network exceptions | ||
| /// } | ||
| /// | ||
| /// return @event; | ||
| /// }); | ||
| /// </code> | ||
| /// </example> | ||
| public static bool IsFromTerminalException(this SentryEvent @event) | ||
| { | ||
| // Check if the original exception was unhandled and not explicitly marked as non-terminal | ||
| if (@event.Exception?.Data[Mechanism.HandledKey] is false) | ||
| { | ||
| // If it's unhandled but explicitly marked as non-terminal, return false | ||
| if (@event.Exception.Data[Mechanism.TerminalKey] is false) | ||
| { | ||
| return false; | ||
| } | ||
| // Otherwise, unhandled exceptions are terminal by default | ||
| return true; | ||
| } | ||
|
|
||
| // Check if any Sentry exceptions are unhandled and terminal | ||
| // (handled: false and terminal: not explicitly false) | ||
| return @event.SentryExceptions?.Any(e => | ||
| e.Mechanism is { Handled: false } && | ||
| e.Mechanism.Terminal != false | ||
| ) ?? false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.