Skip to content

Conversation

@buenaflor
Copy link
Contributor

@buenaflor buenaflor commented Dec 3, 2025

Note: this will mess up issue grouping because it changes the stacktrace


📜 Description

Improve stacktrace of manually captured exceptions on iOS - unhandled exceptions already work well

💡 Motivation and Context

On iOS, when calling captureException() with a Kotlin Throwable, the stack trace shows where captureException() was called, not where the exception was instantiated.

Example

try {
    UserApi.fetchUser()  // Exception thrown HERE
} catch (e: Exception) {
    ErrorHandler.report(e)  // captureException called HERE
}

Expected stack trace: UserApi.fetchUser() → ...
Actual stack trace: ErrorHandler.report() → ...

This works correctly on Android, but not on iOS.

Cause

SentrySDK.captureException() in Sentry Cocoa ignores the NSException's callStackReturnAddresses() and captures a fresh stack trace at call time.

Fix

Instead of calling SentrySDK.captureException(), we now:

  1. Create a SentryEvent directly
  2. Extract the correct stack trace from the NSException using NSExceptionKt_SentryCrashStackCursorFromNSException()
  3. Send via SentrySDK.captureEvent()

This is the same approach already used for unhandled exceptions in SentryUnhandledExceptions.kt (the old way at least).

Fixes #341 and #77

💚 How did you test it?

Currently only manually. I tried running integration and e2e tests but the issue is that the frames are obfuscated so I need to find a way to do an e2e test where dsyms are also uploaded - probably run through one of the examples

Before:
Screenshot 2025-12-03 at 21 41 22


After:
Screenshot 2025-12-03 at 21 49 57

📝 Checklist

You have to check all boxes before merging:

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.

🔮 Next steps

Copy link

@remvst remvst left a comment

Choose a reason for hiding this comment

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

Can't say I'm very familiar with the codebase, but LGTM based on the PR description.

Thank you for taking care of this 🙏

…provements to iOS crash reports and stacktrace handling. Mark the release as "Unreleased" and add warnings regarding potential impacts on issue grouping for iOS events.
@buenaflor buenaflor changed the title [DRAFT] Improve stacktrace of manually captured exceptions on iOS Improve stacktrace of manually captured exceptions on iOS Dec 9, 2025
@buenaflor buenaflor marked this pull request as ready for review December 9, 2025 23:48
@buenaflor buenaflor requested a review from romtsn as a code owner December 9, 2025 23:48
Copy link
Member

@romtsn romtsn left a comment

Choose a reason for hiding this comment

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

Nice work!

@buenaflor buenaflor merged commit 99336dd into main Dec 10, 2025
11 checks passed
@buenaflor buenaflor deleted the improve/call-site-stacktrace-ios branch December 10, 2025 13:43
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.

captureException captures the wrong stack trace

4 participants