diff --git a/CHANGELOG.md b/CHANGELOG.md index 1267478f..1a32805b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - feat: automatically disable `io.sentry.auto-init` ([#93](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/93)) +### Fixes + +- fix: NSNumber to Kotlin Long crash during SentryException conversion ([#92](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/92)) + ### Improvements - ref: improve samples & add SPM docs ([#82](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/82)) diff --git a/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryEventExtensions.kt b/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryEventExtensions.kt index 94674881..73e6d8f3 100644 --- a/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryEventExtensions.kt +++ b/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryEventExtensions.kt @@ -15,8 +15,8 @@ internal fun CocoaSentryEvent.applyKmpEvent(kmpEvent: SentryEvent): CocoaSentryE user = kmpEvent.user?.toCocoaUser() serverName = kmpEvent.serverName dist = kmpEvent.dist - breadcrumbs = kmpEvent.breadcrumbs?.map { it.toCocoaBreadcrumb() }?.toMutableList() - tags = kmpEvent.tags?.toMutableMap() + breadcrumbs = kmpEvent.breadcrumbs.map { it.toCocoaBreadcrumb() }.toMutableList() + tags = kmpEvent.tags.toMutableMap() eventId = SentryId(kmpEvent.eventId.toString()) return this } diff --git a/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryExceptionExtensions.kt b/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryExceptionExtensions.kt index 8308d12d..d811f745 100644 --- a/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryExceptionExtensions.kt +++ b/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryExceptionExtensions.kt @@ -7,5 +7,5 @@ internal fun CocoaSentryException.toKmpSentryException() = SentryException( type = type, value = value, module = module, - threadId = threadId as Long? + threadId = threadId?.longLongValue // longLong represents a 64-bit integer like Kotlin Long ) diff --git a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/FoundationTest.kt b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/FoundationTest.kt index f71ddf37..459ed529 100644 --- a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/FoundationTest.kt +++ b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/FoundationTest.kt @@ -2,6 +2,7 @@ package io.sentry.kotlin.multiplatform import io.sentry.kotlin.multiplatform.extensions.toByteArray import io.sentry.kotlin.multiplatform.extensions.toNSData +import platform.Foundation.NSNumber import platform.Foundation.NSString import platform.Foundation.NSUTF8StringEncoding import platform.Foundation.dataUsingEncoding @@ -24,4 +25,25 @@ class FoundationTest { assertEquals(nsData, byteArray.toNSData()) assertEquals(nsData, nsData.toByteArray().toNSData()) } + + @Test + fun `NSNumber longLong converts to Long correctly`() { + val longValue = 4937446359977427944L + val nsNumber = NSNumber(longLong = longValue) + assertEquals(longValue, nsNumber.longLongValue) + } + + @Test + fun `NSNumber int converts to Long correctly`() { + val intValue = 493744635 + val nsNumber = NSNumber(int = intValue) + assertEquals(intValue.toLong(), nsNumber.longLongValue) + } + + @Test + fun `NSNumber short converts to Long correctly`() { + val shortValue: Short = 4937 + val nsNumber = NSNumber(short = shortValue) + assertEquals(shortValue.toLong(), nsNumber.longLongValue) + } } diff --git a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt new file mode 100644 index 00000000..fbcf2037 --- /dev/null +++ b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt @@ -0,0 +1,64 @@ +package io.sentry.kotlin.multiplatform + +import io.sentry.kotlin.multiplatform.extensions.toKmpSentryException +import io.sentry.kotlin.multiplatform.protocol.SentryException +import kotlinx.cinterop.convert +import platform.Foundation.NSNumber +import kotlin.test.Test + +class SentryExceptionTest { + private val value = "testValue" + private val type = "type" + private val threadId = 1 + + private fun getCocoaSentryException(): CocoaSentryException { + return CocoaSentryException(value = value, type = type) + } + + private fun getKmpSentryException(threadId: Long? = this.threadId.toLong()): SentryException { + return SentryException(value = value, type = type, threadId = threadId) + } + + @Test + fun `SentryException ThreadId NSNumber long conversion`() { + val cocoaSentryException = getCocoaSentryException().apply { + threadId = NSNumber(long = this@SentryExceptionTest.threadId.convert()) + } + val sentryException = getKmpSentryException() + assert(cocoaSentryException.toKmpSentryException() == sentryException) + } + + @Test + fun `SentryException ThreadId NSNumber longLong conversion`() { + val cocoaSentryException = getCocoaSentryException().apply { + threadId = NSNumber(longLong = this@SentryExceptionTest.threadId.convert()) + } + val sentryException = getKmpSentryException() + assert(cocoaSentryException.toKmpSentryException() == sentryException) + } + + @Test + fun `SentryException ThreadId NSNumber int conversion`() { + val cocoaSentryException = getCocoaSentryException().apply { + threadId = NSNumber(int = this@SentryExceptionTest.threadId.convert()) + } + val sentryException = getKmpSentryException() + assert(cocoaSentryException.toKmpSentryException() == sentryException) + } + + @Test + fun `SentryException ThreadId NSNumber short conversion`() { + val cocoaSentryException = getCocoaSentryException().apply { + threadId = NSNumber(short = this@SentryExceptionTest.threadId.convert()) + } + val sentryException = getKmpSentryException() + assert(cocoaSentryException.toKmpSentryException() == sentryException) + } + + @Test + fun `SentryException ThreadId NSNumber null conversion`() { + val cocoaSentryException = getCocoaSentryException() + val sentryException = getKmpSentryException(threadId = null) + assert(cocoaSentryException.toKmpSentryException() == sentryException) + } +}