I am hoping to track any errors that happen on my ...
# kobweb
j
I am hoping to track any errors that happen on my kobweb site, but there are two problems: 1. try/catch is not allowed inside @Composables 2. JS
window.onerror
isn't being triggered, even if I do something like
null!!
. I do see an error message in the js console, with a kotlin-js stack trace, but maybe kotlin isn't propagating it all the way up? Anyone run into this? Any suggestions?
d
I haven't tried this before so I'll double check I'm not screwing something up. But I assume composables run on their own thread (since they are being dispatched as needed by the compose engine) which would make throwing exceptions tricky.
🤔 1
It's weird. I installed a
window.onerror
listener and added this fake crash:
Copy code
TextInput(name, onTextChanged = { name = it; error("onTextChanged") })
and that got handled. Then, in the same page, I added a LaunchedEffect crash:
Copy code
...
LaunchedEffect(Unit) { error("LaunchedEffect") }
var name by remember { mutableStateOf("") }
TextInput(name, onTextChanged = { name = it; error("onTextChanged") })
...
and in that case, both crashes were uncaught and NOT handled by
window.onerror
. I don't know what's going on.
I have no idea how feasible this is but you can write javascript to overwrite the
console.log
call: https://stackoverflow.com/questions/9216441/intercept-calls-to-console-log-in-chrome I wonder if you can intercept
console.error
messages this way...
This definitely looks like a pain point in general in web after doing a bit of search, but unfortunately I don't know much
j
Iiiinteresting... Worth a shot!
c
I believe you'll have to insert a try-catch into all effects (LaunchedEffect, SideEffect, event handlers…). If an exception reaches the Compose Runtime, it crashes and everything dies.