Skip to content

Commit eb4deee

Browse files
Fix: NSNumber conversion to long (#92)
* fix nsnumber conversion to long * fix nsnumber conversion to long * revert sample setup * Format code * add changelog * Add more foundation tests and refactor SentryExceptionTest --------- Co-authored-by: Sentry Github Bot <[email protected]>
1 parent a1d4010 commit eb4deee

File tree

5 files changed

+93
-3
lines changed

5 files changed

+93
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- feat: automatically disable `io.sentry.auto-init` ([#93](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/93))
88

9+
### Fixes
10+
11+
- fix: NSNumber to Kotlin Long crash during SentryException conversion ([#92](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/92))
12+
913
### Improvements
1014

1115
- ref: improve samples & add SPM docs ([#82](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/82))

sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryEventExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ internal fun CocoaSentryEvent.applyKmpEvent(kmpEvent: SentryEvent): CocoaSentryE
1515
user = kmpEvent.user?.toCocoaUser()
1616
serverName = kmpEvent.serverName
1717
dist = kmpEvent.dist
18-
breadcrumbs = kmpEvent.breadcrumbs?.map { it.toCocoaBreadcrumb() }?.toMutableList()
19-
tags = kmpEvent.tags?.toMutableMap()
18+
breadcrumbs = kmpEvent.breadcrumbs.map { it.toCocoaBreadcrumb() }.toMutableList()
19+
tags = kmpEvent.tags.toMutableMap()
2020
eventId = SentryId(kmpEvent.eventId.toString())
2121
return this
2222
}

sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryExceptionExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ internal fun CocoaSentryException.toKmpSentryException() = SentryException(
77
type = type,
88
value = value,
99
module = module,
10-
threadId = threadId as Long?
10+
threadId = threadId?.longLongValue // longLong represents a 64-bit integer like Kotlin Long
1111
)

sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/FoundationTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.sentry.kotlin.multiplatform
22

33
import io.sentry.kotlin.multiplatform.extensions.toByteArray
44
import io.sentry.kotlin.multiplatform.extensions.toNSData
5+
import platform.Foundation.NSNumber
56
import platform.Foundation.NSString
67
import platform.Foundation.NSUTF8StringEncoding
78
import platform.Foundation.dataUsingEncoding
@@ -24,4 +25,25 @@ class FoundationTest {
2425
assertEquals(nsData, byteArray.toNSData())
2526
assertEquals(nsData, nsData.toByteArray().toNSData())
2627
}
28+
29+
@Test
30+
fun `NSNumber longLong converts to Long correctly`() {
31+
val longValue = 4937446359977427944L
32+
val nsNumber = NSNumber(longLong = longValue)
33+
assertEquals(longValue, nsNumber.longLongValue)
34+
}
35+
36+
@Test
37+
fun `NSNumber int converts to Long correctly`() {
38+
val intValue = 493744635
39+
val nsNumber = NSNumber(int = intValue)
40+
assertEquals(intValue.toLong(), nsNumber.longLongValue)
41+
}
42+
43+
@Test
44+
fun `NSNumber short converts to Long correctly`() {
45+
val shortValue: Short = 4937
46+
val nsNumber = NSNumber(short = shortValue)
47+
assertEquals(shortValue.toLong(), nsNumber.longLongValue)
48+
}
2749
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.sentry.kotlin.multiplatform
2+
3+
import io.sentry.kotlin.multiplatform.extensions.toKmpSentryException
4+
import io.sentry.kotlin.multiplatform.protocol.SentryException
5+
import kotlinx.cinterop.convert
6+
import platform.Foundation.NSNumber
7+
import kotlin.test.Test
8+
9+
class SentryExceptionTest {
10+
private val value = "testValue"
11+
private val type = "type"
12+
private val threadId = 1
13+
14+
private fun getCocoaSentryException(): CocoaSentryException {
15+
return CocoaSentryException(value = value, type = type)
16+
}
17+
18+
private fun getKmpSentryException(threadId: Long? = this.threadId.toLong()): SentryException {
19+
return SentryException(value = value, type = type, threadId = threadId)
20+
}
21+
22+
@Test
23+
fun `SentryException ThreadId NSNumber long conversion`() {
24+
val cocoaSentryException = getCocoaSentryException().apply {
25+
threadId = NSNumber(long = this@SentryExceptionTest.threadId.convert())
26+
}
27+
val sentryException = getKmpSentryException()
28+
assert(cocoaSentryException.toKmpSentryException() == sentryException)
29+
}
30+
31+
@Test
32+
fun `SentryException ThreadId NSNumber longLong conversion`() {
33+
val cocoaSentryException = getCocoaSentryException().apply {
34+
threadId = NSNumber(longLong = this@SentryExceptionTest.threadId.convert())
35+
}
36+
val sentryException = getKmpSentryException()
37+
assert(cocoaSentryException.toKmpSentryException() == sentryException)
38+
}
39+
40+
@Test
41+
fun `SentryException ThreadId NSNumber int conversion`() {
42+
val cocoaSentryException = getCocoaSentryException().apply {
43+
threadId = NSNumber(int = this@SentryExceptionTest.threadId.convert())
44+
}
45+
val sentryException = getKmpSentryException()
46+
assert(cocoaSentryException.toKmpSentryException() == sentryException)
47+
}
48+
49+
@Test
50+
fun `SentryException ThreadId NSNumber short conversion`() {
51+
val cocoaSentryException = getCocoaSentryException().apply {
52+
threadId = NSNumber(short = this@SentryExceptionTest.threadId.convert())
53+
}
54+
val sentryException = getKmpSentryException()
55+
assert(cocoaSentryException.toKmpSentryException() == sentryException)
56+
}
57+
58+
@Test
59+
fun `SentryException ThreadId NSNumber null conversion`() {
60+
val cocoaSentryException = getCocoaSentryException()
61+
val sentryException = getKmpSentryException(threadId = null)
62+
assert(cocoaSentryException.toKmpSentryException() == sentryException)
63+
}
64+
}

0 commit comments

Comments
 (0)