Skip to content

Commit 6ec35e3

Browse files
buenaflorromtsn
andauthored
Improve iOS crash reports by adding scope data (#491)
* Update * Update * Update * Update * Update CHANGELOG.md * Update CHANGELOG.md Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com> * set appendCausedBy to true by default when handling ios crashes * Update CHANGELOG for iOS crash reports and SDK bump * Update SentryDependencyContainer.h --------- Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com>
1 parent 25c7d3c commit 6ec35e3

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## 0.22.0
44

5+
### Features
6+
7+
- Improve iOS crash reports by adding scope data ([#491](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/491))
8+
- ⚠️ This change will most likely affect issue grouping as Sentry now properly symbolicates Kotlin iOS crashes
9+
510
### Dependencies
611

712
- Bump Java SDK from v8.25.0 to v8.27.1 ([#487](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/487))

sentry-kotlin-multiplatform/src/appleMain/kotlin/io/sentry/kotlin/multiplatform/nsexception/SentryUnhandledExceptions.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package io.sentry.kotlin.multiplatform.nsexception
1616

1717
import Internal.Sentry.NSExceptionKt_SentryCrashStackCursorFromNSException
1818
import Internal.Sentry.kSentryLevelFatal
19+
import kotlinx.cinterop.invoke
1920
import platform.Foundation.NSException
2021
import platform.Foundation.NSNumber
2122

@@ -54,12 +55,22 @@ internal fun dropKotlinCrashEvent(event: CocoapodsSentryEvent?): CocoapodsSentry
5455
* @see wrapUnhandledExceptionHook
5556
*/
5657
public fun setSentryUnhandledExceptionHook(): Unit = wrapUnhandledExceptionHook { throwable ->
57-
val envelope = throwable.asSentryEnvelope()
58-
// The envelope will be persisted, so we can safely terminate afterwards.
59-
// https://github.com/getsentry/sentry-cocoa/blob/678172142ac1d10f5ed7978f69d16ab03e801057/Sources/Sentry/SentryClient.m#L409
60-
InternalSentrySDK.storeEnvelope(envelope as objcnames.classes.SentryEnvelope)
61-
CocoapodsSentrySDK.configureScope { scope ->
62-
scope?.setTagValue(KOTLIN_CRASH_TAG, KOTLIN_CRASH_TAG)
58+
val crashReporter = InternalSentryDependencyContainer.sharedInstance().crashReporter
59+
val handler = crashReporter.uncaughtExceptionHandler
60+
61+
if (handler != null) {
62+
// This will:
63+
// 1. Write a crash report to disk with ALL synced scope data
64+
// 2. Include tags, user, context, breadcrumbs, etc.
65+
// 3. The crash will be sent on next app launch
66+
handler.invoke(throwable.asNSException(appendCausedBy = true))
67+
} else {
68+
// Fallback to old approach if handler not available
69+
val envelope = throwable.asSentryEnvelope()
70+
InternalSentrySDK.storeEnvelope(envelope as objcnames.classes.SentryEnvelope)
71+
CocoapodsSentrySDK.configureScope { scope ->
72+
scope?.setTagValue(KOTLIN_CRASH_TAG, KOTLIN_CRASH_TAG)
73+
}
6374
}
6475
}
6576

sentry-kotlin-multiplatform/src/nativeInterop/cinterop/Sentry.Internal.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ language = Objective-C
22
headers = SentryClient.h SentryEvent.h SentryDebugImageProvider.h SentryHub.h SentryScope.h \
33
SentryCrashMonitor_NSException.h SentryCrashMonitor_NSException+NSExceptionKt.h \
44
SentryCrashStackCursor.h SentryDependencyContainer.h SentryHook.h SentrySDKInternal.h \
5-
SentryStacktraceBuilder.h SentryThreadInspector.h PrivateSentrySDKOnly.h \
5+
SentryStacktraceBuilder.h SentryThreadInspector.h PrivateSentrySDKOnly.h SentryCrash.h \
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// SentryCrash interface - expose the uncaughtExceptionHandler
2+
// Based on: https://github.com/getsentry/sentry-cocoa/blob/main/Sources/Sentry/include/SentryCrash.h
3+
4+
@interface SentryCrash : NSObject
5+
6+
@property (nonatomic, assign, nullable) NSUncaughtExceptionHandler *uncaughtExceptionHandler;
7+
8+
@end

sentry-kotlin-multiplatform/src/nativeInterop/cinterop/SentryInternal/SentryDependencyContainer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
#import <Foundation/Foundation.h>
2020
#import <SentryDebugImageProvider.h>
21+
#import <SentryCrash.h>
2122

2223
@interface SentryDependencyContainer : NSObject
2324

2425
+ (nonnull instancetype)sharedInstance;
2526

2627
@property (nonatomic, strong, nonnull) SentryDebugImageProvider *debugImageProvider;
28+
@property (nonatomic, strong, nonnull) SentryCrash *crashReporter;
2729

2830
@end

0 commit comments

Comments
 (0)