2

Is there a proper way in Kotlin-js to globally catch unhandled exception?

I have made a general attempt with window.onerror (see below). The only reported error is on non_existing_function2, but nothing is reported for the other 2 cases.

Any guidance? Thanks

window.onerror = { msg, url, line, col, error ->
   window.alert(“Error: $msg \nurl: $url\nline: $line\ncol: $col\nerror: $error”)
   true
}
async {
   throw Exception(“generated”)
}
async {
   js(“non_existing_function1();”)
}

js("non_existing_function2();")
4

1 回答 1

3

您使用的asyncKotlin中定义为

fun <T> async(block: suspend CoroutineScope.() -> T): Deferred<T>

WhereDeferred是有状态的,不会在任何地方传播异常。您可以使用async { ... }.asPromise()桥接到JavaScript并传播拒绝以正确处理它。

于 2018-06-02T08:58:59.353 回答