@@ -16,6 +16,7 @@ package io.sentry.kotlin.multiplatform.nsexception
1616
1717import Internal.Sentry.NSExceptionKt_SentryCrashStackCursorFromNSException
1818import Internal.Sentry.kSentryLevelFatal
19+ import io.sentry.kotlin.multiplatform.CocoaSentryLevel
1920import kotlinx.cinterop.invoke
2021import platform.Foundation.NSException
2122import 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 */
126137private 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