https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
b

Benoît

03/22/2021, 10:45 AM
Using Crashlytics with KMP and coroutines, the stack trace is completely lost, there's no way for us to what happened, we only know there was a crash in the
CoroutineExceptionHandler
Swift object Has anyone managed to fix this issue?
m

Michal Klimczak

03/22/2021, 11:29 AM
there isn't a direct solution but w workaround https://github.com/touchlab/CrashKiOS
b

Benoît

03/22/2021, 11:38 AM
Thanks a lot! I'll check this out
b

bsimmons

03/22/2021, 11:39 AM
In my experience, I've found that thorough unit testing and generous logging are often more effective than the exceptions you get on iOS when working between threads. Are you using supervised coroutine by any chance?
b

Benoît

03/23/2021, 9:31 PM
No I have a CoroutineExceptionHandler implemented on iOS which sends a crash report to Crashlytics. But I rely on the fact that the code won’t crash because I’m using functional programming, the code doesn’t rely on Exceptions to propagate errors
I use the exception handler mainly to track any unintended Exception thrown and wrap then wrap it into an Either
b

bsimmons

03/24/2021, 1:01 PM
Ok, I think I see what the problem is. If you are trying to deal with exceptions explicitly on the Swift side of things, then they'll get mangled. Personally, I catch all exceptions in the common Kotlin code part, and convert them to strings there. On the Swift side, I just use a generic
NSError
to log a non-fatal exception, but right before, I log the real stack trace as a string in the log.
b

Benoît

03/24/2021, 2:40 PM
I am using
stackTraceToString()
from the standard library, I do it in the Kotlin code. The Swift Code receives the Throwable and calls a helper function I've made:
Copy code
fun exceptionToString(exception: Throwable): String = exception.freezeObject().stackTraceToString()
However this isn't useful, the String it gives me looks like that:
Copy code
kotlin.NotImplementedError: An operation is not implemented. at 0 shared 0x00000001014dd688 shared + 382600 at 1 shared 0x00000001014d6810 shared + 354320 at 2 shared 0x00000001014ca470 shared + 304240 at 3 shared 0x00000001014ca574 shared + 304500 at 4 shared 0x0000000101943c28 shared + 4996136 at 5 shared 0x0000000101945ca4 shared + 5004452 at 6 shared 0x0000000101917688 shared + 4814472 at 7 shared 0x00000001019187ec shared + 4818924 at 8 shared 0x0000000101686ca0 shared + 2124960 at 9 shared 0x0000000101688518 shared + 2131224 at 10 shared 0x0000000101688808 shared + 2131976 at 11 shared 0x00000
b

bsimmons

03/24/2021, 3:01 PM
Yeah, I would avoid sending the exception to kotlin altogether. If you are using
Either
then wrap your Kotlin code in
runCatching
and convert it to an
Either<String, MyData>
and pass back the exception as a string stack trace. Then in Swift you avoid exceptions altogether and already have the formatted string.
b

Benoît

03/24/2021, 3:10 PM
I have a lot of things wrapped into a Either.catch block but you never know what can happen, if there's an unexpected crash on iOS I would like to be able to know about it, just so I can add another catch block
But it's fine I think CrashKiOS will do the trick :)
👍 1
Thanks a lot for your help
81 Views