NSExceptionKt
A Kotlin Multiplatform Library to improve crash reports on Apple platforms.
Installation
Checkout the implementation specific READMEs for usage and installation details:
Why this library?
If you have been developing applications for Apple platforms with Kotlin Native,
then you have likely encountered crashes like the following:
So far so good. You fix the crash and all is well. But what happens when such a crash occurs in production?!
Well, just like the above message said, the program will be terminated.
And that is probably what you want anyway, so you open your favorite crash reporting tool and...:
The good news: you know your app crashed 🥸. The bad news: there is no useful stacktrace 🥹.
Fortunately we can log
such unhandled exceptions in Kotlin before the app is terminated!
The challenges
While we can use any Objective-C crash reporting SDK we like,
the concept of error handling is very different between Kotlin and ObjC/Swift.
On the Kotlin side all exceptions are unchecked, but on the Swift side they are all checked.
That's why we need that @Throws annotation,
which tells Kotlin what exception types a function is expected to throw.
When you try to log your unhandled Exceptions, you'll soon realise that most SDKs only expose APIs to log NSErrors.
Unfortunately you can't just map an Exception to a NSError, for one you would lose the stacktrace.
In the rare case that you can log your exception (e.g. with Crashlytics via the ExceptionModel class),
the logged exception will be marked as non-fatal and the real crash is logged as well (resulting in two reports).
So we basically need to:
- log a fatal error from Kotlin
- attach a stacktrace to it
- preferably attach the caused by exceptions (with their stacktrace)
- prevent the Kotlin termination from being logged
Acknowledgments
Kermit by Touchlab
and specifically @kpgalligan have been a great inspiration for this project.
I would also like to thank Firebase, Bugsnag and Sentry for publishing the sources of their SDKs.
Without those sources projects like these wouldn't exist.