Skip to content

Instantly share code, notes, and snippets.

@SUPERCILEX
Last active December 31, 2018 22:14
Show Gist options
  • Save SUPERCILEX/f4b01ccf6fd4ef7ec0a85dbd59c89d6c to your computer and use it in GitHub Desktop.
Save SUPERCILEX/f4b01ccf6fd4ef7ec0a85dbd59c89d6c to your computer and use it in GitHub Desktop.
@Keep
internal class LoggingHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler),
CoroutineExceptionHandler {
override fun handleException(context: CoroutineContext, exception: Throwable) {
Crashlytics.logException(exception)
// Since we don't want to crash and Coroutines will call the current thread's handler, we
// install a noop handler and then reinstall the existing one once coroutines calls the new
// handler.
Thread.currentThread().apply {
// _Do_ crash the main thread to ensure we're not left in a bad state
if (isMain) return@apply
val removed = uncaughtExceptionHandler
uncaughtExceptionHandler = if (removed == null) {
ResettingHandler
} else {
Thread.UncaughtExceptionHandler { t, _ ->
t.uncaughtExceptionHandler = removed
}
}
}
}
private object ResettingHandler : Thread.UncaughtExceptionHandler {
override fun uncaughtException(t: Thread, e: Throwable) {
t.uncaughtExceptionHandler = null
}
}
}
val Thread.isMain get() = this === Looper.getMainLooper().thread
# Put in file src/main/resources/META-INF/services/kotlinx.coroutines.CoroutineExceptionHandler
com.supercilex.robotscouter.core.LoggingHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment