Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change log for kotlinx.coroutines

## Version 1.10.3
* `propagateExceptionFinalResort` now throws instead of logging on the `js` and `wasmJS` targets.

## Version 1.10.2

* Fixed the `kotlinx-coroutines-debug` JAR file including the `module-info.class` file twice, resulting in failures in various tooling (#4314). Thanks, @RyuNen344!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ import kotlin.js.unsafeCast
internal actual external interface JsAny

internal actual fun Throwable.toJsException(): JsAny = this.unsafeCast<JsAny>()

internal actual fun propagateExceptionFinalResort(exception: Throwable) {
throwAsync(exception.toJsException())
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class PropagateExceptionFinalResortTest : TestBase() {
@BeforeTest
private fun removeListeners() {
// Remove a Node.js's internal listener, which prints the exception to stdout.
// https://nodejs.org/api/process.html#event-uncaughtexception
js("""
globalThis.originalListeners = process.listeners('uncaughtException');
process.removeAllListeners('uncaughtException');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@ internal expect fun Throwable.toJsException(): JsAny
* 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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package kotlinx.coroutines.internal

internal actual typealias JsAny = kotlin.js.JsAny

internal external object globalThis : JsAny {
val reportError: ((error: JsAny) -> Unit)?
}

internal actual fun Throwable.toJsException(): JsAny =
toJsError(message, this::class.simpleName, stackTraceToString())

Expand All @@ -14,3 +18,15 @@ internal fun toJsError(message: String?, className: String?, stack: String?): Js
return error;
""")
}

internal actual fun propagateExceptionFinalResort(exception: Throwable) {
val jsException = exception.toJsException()
// https://github.com/JetBrains/kotlin-wrappers/blob/master/kotlin-browser/src/webMain/generated/web/errors/reportError.kt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using commit hashes instead of branch names can make such links more reliable. I wanted to say "this link could start returning 404 tomorrow", but in fact, it returns 404 for me today!

if (globalThis.reportError != null) {
// Modern browsers, Deno, Bun, etc.
globalThis.reportError(jsException)
Comment on lines +24 to +27
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, we can only rely on reportError once your change in kotlin-wrappers is commited to master? So this PR is dependant on your merge.

https://github.com/JetBrains/kotlin-wrappers/blob/3d7419234a45f7f8a31ea9274f6909ecd63734c6/kotlin-browser/src/webMain/generated/web/errors/reportError.kt

} else {
// Old Safari, Node.js
throwAsync(jsException)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class PropagateExceptionFinalResortTest : TestBase() {
}

/*
* Test that `propagateExceptionFinalResort` re-throws the exception on Wasm/JS.
* Test that `propagateExceptionFinalResort` re-throws the exception on wasmJS.
*
* It is checked by setting up an exception handler within Wasm/JS.
* It is checked by setting up an exception handler within wasmJS.
*/
@Test
fun testPropagateExceptionFinalResortReThrowsOnWasmJS() = runTest {
Expand All @@ -31,6 +31,7 @@ class PropagateExceptionFinalResortTest : TestBase() {
}

private fun addUncaughtExceptionHandlerHelper() {
// https://nodejs.org/api/process.html#event-uncaughtexception
js("""
globalThis.exceptionCaught = false;
globalThis.exceptionHandler = function(e) {
Expand Down