Skip to content

Add public API for checking unhandled exceptions#5177

Draft
jamescrosswell wants to merge 6 commits intomainfrom
fix/issue-2877-unhandled-exception-api
Draft

Add public API for checking unhandled exceptions#5177
jamescrosswell wants to merge 6 commits intomainfrom
fix/issue-2877-unhandled-exception-api

Conversation

@jamescrosswell
Copy link
Copy Markdown
Collaborator

@jamescrosswell jamescrosswell commented May 1, 2026

Summary

Adds two new public extension methods to SentryEvent:

  • IsFromUnhandledException(): Checks if the event is from any unhandled exception
  • IsFromTerminalException(): Checks if the event is from a terminal (crash-causing) exception

Motivation

Fixes #2877

Users need to filter events in callbacks like BeforeSend based on whether exceptions are unhandled/terminal. This is particularly useful in MAUI apps where you might want to filter out common exceptions (like network timeouts) but always capture them if they're terminal.

Implementation

Following @bruno-garcia's feedback, this uses extension methods rather than making internal methods public. The methods check both Exception.Data flags and SentryExceptions mechanism properties.

Key differences:

  • IsFromUnhandledException: Returns true for any unhandled exception (terminal or non-terminal)
  • IsFromTerminalException: Returns true only for unhandled exceptions that are terminal (caused app crash)

Both are well-documented with XML docs and usage examples.

🤖 Generated with Claude Code

jamescrosswell and others added 2 commits May 1, 2026 13:12
Adds two new public extension methods to SentryEvent:
- IsFromUnhandledException(): Checks if event is from any unhandled exception
- IsFromTerminalException(): Checks if event is from a terminal (crash-causing) exception

This addresses the need to filter events based on whether they're unhandled/terminal,
particularly useful in MAUI apps where users want to always capture terminal exceptions
even if they match other filters (e.g., network timeouts).

The implementation uses extension methods as suggested by @bruno-garcia, providing a
clean public API without exposing internal methods. Both methods check Exception.Data
and SentryExceptions for the handled/terminal flags.

Fixes #2877

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 4, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.08%. Comparing base (1d43862) to head (d085338).

Files with missing lines Patch % Lines
src/Sentry/SentryEventExtensions.cs 72.72% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5177   +/-   ##
=======================================
  Coverage   74.08%   74.08%           
=======================================
  Files         506      507    +1     
  Lines       18247    18255    +8     
  Branches     3564     3568    +4     
=======================================
+ Hits        13519    13525    +6     
  Misses       3858     3858           
- Partials      870      872    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jamescrosswell jamescrosswell marked this pull request as ready for review May 5, 2026 02:27
@jamescrosswell jamescrosswell requested a review from Flash0ver as a code owner May 5, 2026 02:27
Comment thread src/Sentry/SentryEventExtensions.cs
…n methods

HasUnhandledException() and HasUnhandledNonTerminalException() duplicated
the logic in the new public extension methods. Removed the private methods
and updated GetExceptionType() to call IsFromUnhandledException() and
IsFromTerminalException() directly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8fc8368. Configure here.

Comment thread src/Sentry/SentryEvent.cs Outdated
IsFromUnhandledException() && !IsFromTerminalException() is not equivalent
to the original per-exception AND check. When SentryExceptions contains a
mix of terminal and non-terminal unhandled exceptions, IsFromTerminalException()
returns true (finding the terminal one), incorrectly suppressing the
UnhandledNonTerminal result.

Restore the private method with its original body. HasUnhandledException()
remains replaced by the public IsFromUnhandledException() extension method,
which is a true semantic equivalent.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jamescrosswell jamescrosswell marked this pull request as draft May 7, 2026 00:37
@jamescrosswell jamescrosswell marked this pull request as ready for review May 7, 2026 04:52
@jamescrosswell jamescrosswell marked this pull request as draft May 7, 2026 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make SentryEvent.HasTerminalException() public

1 participant