Skip to content

Commit 9fc534f

Browse files
committed
Update
1 parent 6ec35e3 commit 9fc534f

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

sentry-kotlin-multiplatform/src/appleMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.apple.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package io.sentry.kotlin.multiplatform
22

33
import Internal.Sentry.PrivateSentrySDKOnly
4+
import Internal.Sentry.kSentryLevelError
45
import cocoapods.Sentry.SentrySDK
56
import io.sentry.kotlin.multiplatform.extensions.toCocoaBreadcrumb
67
import io.sentry.kotlin.multiplatform.extensions.toCocoaUser
78
import io.sentry.kotlin.multiplatform.extensions.toCocoaUserFeedback
8-
import io.sentry.kotlin.multiplatform.nsexception.asNSException
9+
import io.sentry.kotlin.multiplatform.nsexception.asSentryEvent
910
import io.sentry.kotlin.multiplatform.nsexception.dropKotlinCrashEvent
1011
import io.sentry.kotlin.multiplatform.protocol.Breadcrumb
1112
import io.sentry.kotlin.multiplatform.protocol.SentryId
@@ -75,15 +76,22 @@ internal actual class SentryBridge actual constructor(private val sentryInstance
7576
}
7677

7778
actual fun captureException(throwable: Throwable): SentryId {
78-
val cocoaSentryId = SentrySDK.captureException(throwable.asNSException(true))
79+
val event = throwable.asSentryEvent(
80+
level = kSentryLevelError,
81+
isHandled = true,
82+
markThreadAsCrashed = false
83+
)
84+
val cocoaSentryId = SentrySDK.captureEvent(event)
7985
return SentryId(cocoaSentryId.toString())
8086
}
8187

8288
actual fun captureException(throwable: Throwable, scopeCallback: ScopeCallback): SentryId {
83-
val cocoaSentryId = SentrySDK.captureException(
84-
throwable.asNSException(true),
85-
configureScopeCallback(scopeCallback)
89+
val event = throwable.asSentryEvent(
90+
level = kSentryLevelError,
91+
isHandled = true,
92+
markThreadAsCrashed = false
8693
)
94+
val cocoaSentryId = SentrySDK.captureEvent(event, configureScopeCallback(scopeCallback))
8795
return SentryId(cocoaSentryId.toString())
8896
}
8997

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

Lines changed: 21 additions & 9 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 io.sentry.kotlin.multiplatform.CocoaSentryLevel
1920
import kotlinx.cinterop.invoke
2021
import platform.Foundation.NSException
2122
import platform.Foundation.NSNumber
@@ -96,19 +97,29 @@ private fun Throwable.asSentryEnvelope(): CocoapodsSentryEnvelope {
9697

9798
/**
9899
* Converts `this` [Throwable] to a [cocoapods.Sentry.SentryEvent].
100+
*
101+
* @param level The Sentry level (e.g., kSentryLevelFatal for crashes, kSentryLevelError for handled)
102+
* @param isHandled Whether this is a handled exception (false for crashes, true for captured exceptions)
103+
* @param markThreadAsCrashed Whether to mark the current thread as crashed (true for crashes, false for handled)
99104
*/
100105
@Suppress("UnnecessaryOptInAnnotation")
101-
private fun Throwable.asSentryEvent(): CocoapodsSentryEvent =
102-
CocoapodsSentryEvent(kSentryLevelFatal).apply {
106+
internal fun Throwable.asSentryEvent(
107+
level: CocoaSentryLevel = kSentryLevelFatal,
108+
isHandled: Boolean = false,
109+
markThreadAsCrashed: Boolean = true
110+
): CocoapodsSentryEvent =
111+
CocoapodsSentryEvent(level).apply {
103112
@Suppress("UNCHECKED_CAST")
104113
val threads =
105114
threadInspector?.getCurrentThreadsWithStackTrace() as List<CocoapodsSentryThread>?
106115
this.threads = threads
107116
val currentThread = threads?.firstOrNull { it.current?.boolValue ?: false }?.apply {
108-
setCrashed(NSNumber(true))
109-
// Crashed threads shouldn't have a stacktrace, the thread_id should be set on the exception instead
110-
// https://develop.sentry.dev/sdk/event-payloads/threads/
111-
stacktrace = null
117+
if (markThreadAsCrashed) {
118+
setCrashed(NSNumber(true))
119+
// Crashed threads shouldn't have a stacktrace, the thread_id should be set on the exception instead
120+
// https://develop.sentry.dev/sdk/event-payloads/threads/
121+
stacktrace = null
122+
}
112123
}
113124
debugMeta = threads?.let {
114125
InternalSentryDependencyContainer.sharedInstance().debugImageProvider.getDebugImagesForThreads(
@@ -117,18 +128,19 @@ private fun Throwable.asSentryEvent(): CocoapodsSentryEvent =
117128
}
118129
exceptions = this@asSentryEvent
119130
.let { throwable -> throwable.causes.asReversed() + throwable }
120-
.map { it.asNSException().asSentryException(currentThread?.threadId) }
131+
.map { it.asNSException().asSentryException(currentThread?.threadId, isHandled) }
121132
}
122133

123134
/**
124135
* Converts `this` [NSException] to a [io.sentry.kotlin.multiplatform.protocol.SentryException].
125136
*/
126137
private fun NSException.asSentryException(
127-
threadId: NSNumber?
138+
threadId: NSNumber?,
139+
isHandled: Boolean = false
128140
): CocoapodsSentryException = CocoapodsSentryException(reason ?: "", name ?: "Throwable").apply {
129141
this.threadId = threadId
130142
mechanism = CocoapodsSentryMechanism("generic").apply {
131-
setHandled(NSNumber(false))
143+
setHandled(NSNumber(isHandled))
132144
}
133145
stacktrace = threadInspector?.stacktraceBuilder?.let { stacktraceBuilder ->
134146
val cursor = NSExceptionKt_SentryCrashStackCursorFromNSException(this@asSentryException)

0 commit comments

Comments
 (0)