kotlinx.coroutines icon indicating copy to clipboard operation
kotlinx.coroutines copied to clipboard

Public API to configure a default exception handler

Open swankjesse opened this issue 2 years ago • 3 comments

Use case

I’m using Kotlin/JS. I’d like a public API to install a platform exception handler. As-is my uncaught exceptions are going to console.log, where they’re invisible to my observability tools. The ensurePlatformExceptionHandlerLoaded() function does what I want, but that function isn’t public!

I have tried to get my CoroutineExceptionHandler into every CoroutineContext in my application, but I’ve found it very difficult to stay on top of all of these. Especially with first- and third-party libraries that do their own coroutines things!

The Shape of the API

This multiplatform API addresses my request across all platforms.

package kotlinx.coroutines

/**
 * Invoked for uncaught exceptions thrown in CoroutineContexts that
 * don’t have a CoroutineExceptionHandler.
 * 
 * If null, a platform-specific handler will be invoked instead.
 */
val defaultCoroutineExceptionHandler: CoroutineExceptionHandler? = null

Prior Art

Java has this:

class Thread {
  public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler e)
  public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
}

swankjesse avatar Dec 08 '23 16:12 swankjesse

Should be solved by #2932

dovchinnikov avatar Dec 11 '23 19:12 dovchinnikov