diff --git a/kotlinx-coroutines-core/common/src/CoroutineExceptionHandler.kt b/kotlinx-coroutines-core/common/src/CoroutineExceptionHandler.kt index 0899eb6fb6..8c174e2a96 100644 --- a/kotlinx-coroutines-core/common/src/CoroutineExceptionHandler.kt +++ b/kotlinx-coroutines-core/common/src/CoroutineExceptionHandler.kt @@ -67,7 +67,7 @@ public inline fun CoroutineExceptionHandler(crossinline handler: (CoroutineConte * with the corresponding exception when the handler is called. Normally, the handler is used to * log the exception, show some kind of error message, terminate, and/or restart the application. * - * If you need to handle exception in a specific part of the code, it is recommended to use `try`/`catch` around + * If you need to handle the exception in a specific part of the code, it is recommended to use `try`/`catch` around * the corresponding code inside your coroutine. This way you can prevent completion of the coroutine * with the exception (exception is now _caught_), retry the operation, and/or take other arbitrary actions: * @@ -83,14 +83,15 @@ public inline fun CoroutineExceptionHandler(crossinline handler: (CoroutineConte * * ### Uncaught exceptions with no handler * - * When no handler is installed, exception are handled in the following way: - * - If exception is [CancellationException], it is ignored, as these exceptions are used to cancel coroutines. + * When no handler is installed, an exception is handled in the following way: + * - If the exception is [CancellationException], it is ignored, as these exceptions are used to cancel coroutines. * - Otherwise, if there is a [Job] in the context, then [Job.cancel] is invoked. * - Otherwise, as a last resort, the exception is processed in a platform-specific manner: * - On JVM, all instances of [CoroutineExceptionHandler] found via [ServiceLoader], as well as * the current thread's [Thread.uncaughtExceptionHandler], are invoked. * - On Native, the whole application crashes with the exception. - * - On JS, the exception is logged via the Console API. + * - On JS and Wasm/JS, the exception is propagated into the event loop + * and is processed in a platform-specific way determined by the platform itself. * * [CoroutineExceptionHandler] can be invoked from an arbitrary thread. */ @@ -102,7 +103,7 @@ public interface CoroutineExceptionHandler : CoroutineContext.Element { /** * Handles uncaught [exception] in the given [context]. It is invoked - * if coroutine has an uncaught exception. + * if the coroutine has an uncaught exception. */ public fun handleException(context: CoroutineContext, exception: Throwable) } diff --git a/kotlinx-coroutines-core/common/src/internal/CoroutineExceptionHandlerImpl.common.kt b/kotlinx-coroutines-core/common/src/internal/CoroutineExceptionHandlerImpl.common.kt index 25a3a2684d..a96621ffe9 100644 --- a/kotlinx-coroutines-core/common/src/internal/CoroutineExceptionHandlerImpl.common.kt +++ b/kotlinx-coroutines-core/common/src/internal/CoroutineExceptionHandlerImpl.common.kt @@ -15,7 +15,7 @@ internal expect val platformExceptionHandlers: Collection() diff --git a/kotlinx-coroutines-core/js/test/PropagateExceptionFinalResortTest.kt b/kotlinx-coroutines-core/js/test/PropagateExceptionFinalResortTest.kt new file mode 100644 index 0000000000..33efb7eff2 --- /dev/null +++ b/kotlinx-coroutines-core/js/test/PropagateExceptionFinalResortTest.kt @@ -0,0 +1,50 @@ +package kotlinx.coroutines + +import kotlinx.coroutines.testing.* +import kotlin.js.* +import kotlin.test.* + +class PropagateExceptionFinalResortTest : TestBase() { + @BeforeTest + private fun removeListeners() { + // Remove a Node.js's internal listener, which prints the exception to stdout. + js(""" + globalThis.originalListeners = process.listeners('uncaughtException'); + process.removeAllListeners('uncaughtException'); + """) + } + + @AfterTest + private fun restoreListeners() { + js(""" + if (globalThis.originalListeners) { + process.removeAllListeners('uncaughtException'); + globalThis.originalListeners.forEach(function(listener) { + process.on('uncaughtException', listener); + }); + } + """) + } + + /* + * Test that `propagateExceptionFinalResort` re-throws the exception on JS. + * + * It is checked by setting up an exception handler within JS. + */ + @Test + fun testPropagateExceptionFinalResortReThrowsOnNodeJS() = runTest { + js(""" + globalThis.exceptionCaught = false; + process.on('uncaughtException', function(e) { + globalThis.exceptionCaught = true; + }); + """) + val job = GlobalScope.launch { + throw IllegalStateException("My ISE") + } + job.join() + delay(1) // Let the exception be re-thrown and handled. + val exceptionCaught = js("globalThis.exceptionCaught") as Boolean + assertTrue(exceptionCaught) + } +} diff --git a/kotlinx-coroutines-core/jsAndWasmJsShared/src/internal/CoroutineExceptionHandlerImpl.kt b/kotlinx-coroutines-core/jsAndWasmJsShared/src/internal/CoroutineExceptionHandlerImpl.kt new file mode 100644 index 0000000000..8f3bbd56e9 --- /dev/null +++ b/kotlinx-coroutines-core/jsAndWasmJsShared/src/internal/CoroutineExceptionHandlerImpl.kt @@ -0,0 +1,17 @@ +package kotlinx.coroutines.internal + +import kotlinx.coroutines.* + +internal expect interface JsAny + +internal expect fun Throwable.toJsException(): JsAny + +/* + * Schedule an exception to be thrown inside JS or Wasm/JS event loop, + * rather than in the current execution branch. + */ +internal fun throwAsync(e: JsAny): Unit = js("setTimeout(function () { throw e }, 0)") + +internal actual fun propagateExceptionFinalResort(exception: Throwable) { + throwAsync(exception.toJsException()) +} diff --git a/kotlinx-coroutines-core/wasmJs/src/internal/CoroutineExceptionHandlerImpl.kt b/kotlinx-coroutines-core/wasmJs/src/internal/CoroutineExceptionHandlerImpl.kt index b3c09e7c38..52c9a827a1 100644 --- a/kotlinx-coroutines-core/wasmJs/src/internal/CoroutineExceptionHandlerImpl.kt +++ b/kotlinx-coroutines-core/wasmJs/src/internal/CoroutineExceptionHandlerImpl.kt @@ -1,8 +1,16 @@ package kotlinx.coroutines.internal -import kotlinx.coroutines.* +internal actual typealias JsAny = kotlin.js.JsAny -internal actual fun propagateExceptionFinalResort(exception: Throwable) { - // log exception - console.error(exception.toString()) -} \ No newline at end of file +internal actual fun Throwable.toJsException(): JsAny = + toJsError(message, this::class.simpleName, stackTraceToString()) + +internal fun toJsError(message: String?, className: String?, stack: String?): JsAny { + js(""" + const error = new Error(); + error.message = message; + error.name = className; + error.stack = stack; + return error; + """) +} diff --git a/kotlinx-coroutines-core/wasmJs/test/PropagateExceptionFinalResortTest.kt b/kotlinx-coroutines-core/wasmJs/test/PropagateExceptionFinalResortTest.kt new file mode 100644 index 0000000000..2449b72760 --- /dev/null +++ b/kotlinx-coroutines-core/wasmJs/test/PropagateExceptionFinalResortTest.kt @@ -0,0 +1,49 @@ +package kotlinx.coroutines + +import kotlinx.coroutines.testing.TestBase +import kotlin.test.* + +class PropagateExceptionFinalResortTest : TestBase() { + @BeforeTest + private fun addUncaughtExceptionHandler() { + addUncaughtExceptionHandlerHelper() + } + + @AfterTest + private fun removeHandler() { + removeHandlerHelper() + } + + /* + * Test that `propagateExceptionFinalResort` re-throws the exception on Wasm/JS. + * + * It is checked by setting up an exception handler within Wasm/JS. + */ + @Test + fun testPropagateExceptionFinalResortReThrowsOnWasmJS() = runTest { + val job = GlobalScope.launch { + throw IllegalStateException("My ISE") + } + job.join() + delay(1) // Let the exception be re-thrown and handled. + assertTrue(exceptionCaught()) + } +} + +private fun addUncaughtExceptionHandlerHelper() { + js(""" + globalThis.exceptionCaught = false; + globalThis.exceptionHandler = function(e) { + globalThis.exceptionCaught = true; + }; + process.on('uncaughtException', globalThis.exceptionHandler); + """) +} + +private fun removeHandlerHelper() { + js(""" + process.removeListener('uncaughtException', globalThis.exceptionHandler); + """) +} + +private fun exceptionCaught(): Boolean = js("globalThis.exceptionCaught")