Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Razhan/3deee9cdfcb690892874258107a9fd39 to your computer and use it in GitHub Desktop.
Save Razhan/3deee9cdfcb690892874258107a9fd39 to your computer and use it in GitHub Desktop.

Revisions

  1. @SUPERCILEX SUPERCILEX revised this gist Oct 20, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion coroutines-global-handler.kt
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    internal class LoggingHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler),
    CoroutineExceptionHandler {
    override fun handleException(context: CoroutineContext, exception: Throwable) {
    CrashLogger.invoke(exception)
    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
    @@ -28,3 +28,5 @@ internal class LoggingHandler : AbstractCoroutineContextElement(CoroutineExcepti
    }
    }
    }

    val Thread.isMain get() = this === Looper.getMainLooper().thread
  2. @SUPERCILEX SUPERCILEX revised this gist Oct 20, 2018. 1 changed file with 26 additions and 2 deletions.
    28 changes: 26 additions & 2 deletions coroutines-global-handler.kt
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,30 @@
    @Keep
    internal class LoggingHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler),
    CoroutineExceptionHandler {
    override fun handleException(context: CoroutineContext, exception: Throwable) =
    CrashLogger.invoke(exception)
    override fun handleException(context: CoroutineContext, exception: Throwable) {
    CrashLogger.invoke(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
    }
    }
    }
  3. @SUPERCILEX SUPERCILEX revised this gist Oct 20, 2018. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions service.txt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,2 @@
    # Put in file module/resources/META-INF/services/kotlinx.coroutines.CoroutineExceptionHandler
    # Tip: `src` in on the same level as `resources`
    # Put in file src/main/resources/META-INF/services/kotlinx.coroutines.CoroutineExceptionHandler
    com.supercilex.robotscouter.core.LoggingHandler
  4. @SUPERCILEX SUPERCILEX created this gist Oct 20, 2018.
    6 changes: 6 additions & 0 deletions coroutines-global-handler.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    @Keep
    internal class LoggingHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler),
    CoroutineExceptionHandler {
    override fun handleException(context: CoroutineContext, exception: Throwable) =
    CrashLogger.invoke(exception)
    }
    3 changes: 3 additions & 0 deletions service.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # Put in file module/resources/META-INF/services/kotlinx.coroutines.CoroutineExceptionHandler
    # Tip: `src` in on the same level as `resources`
    com.supercilex.robotscouter.core.LoggingHandler