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?

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

@github-actions
Copy link
Contributor

github-actions bot commented Dec 3, 2025

Fails
🚫 Please consider adding a changelog entry for the next release.

Instructions and example for changelog

Please add an entry to CHANGELOG.md to the "Unreleased" section. Make sure the entry includes this PR's number.

Example:

## Unreleased

### Features

- [DRAFT] Improve stacktrace of manually captured exceptions on iOS ([#493](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/493))

If none of the above apply, you can opt out of this check by adding #skip-changelog to the PR description or adding a skip-changelog label.

Generated by 🚫 dangerJS against 9fc534f

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

2 participants