Hi! I'm working on a browser extension, and am finding that when the Promise returned by browser.tabs.executeScript fails, the exception thrown by await() just gets logged instead of being captured by a surrounding catch block. Here's a snippet:
Copy code
suspend fun executeScript(frameId: Int): String {
val result = js("browser.tabs.executeScript({ frameId: frameId, code: 'window.location.href' })") as Promise<String>
return try {
result.await()
} catch (t: Throwable) {
// Never gets invoked
console.error("Got error in await:", t.message)
""
}
}
👍 1
Jeff Davidson
06/04/2021, 4:49 PM
If I change the JS to:
Copy code
Promise.reject(new Error('fail'))
then the catch block gets invoked. Also, I can capture the error by calling .catch() on the Promise. But shouldn't await() throw a Throwable that can be caught here, instead of just aborting execution and logging the error?
Jeff Davidson
06/04/2021, 5:04 PM
I think the problem is that this API rejects the promise with a plain object rather than an error, and so Kotlin fails to map that to a Throwable?
Jeff Davidson
06/04/2021, 5:13 PM
yeah, that's exactly it. it seems to just be a plain object with a message field, as opposed to an Error. is it a bug that Kotlin assumes it will be an Error (that I assume can be mapped cleanly to a Throwable)?