unlike what one may expect, the following code will not trigger the window.error handler
window.addEventListener("error",function(error){
alert("uncaught error somewhere!");
});
(async function(){
let foo= ((await ( await fetch("/no_json_plz_i_want_an_error")).json()));
})();
(reason being "because the error occurs in a promise context" according to a comment on this question: why doesn't fetch().json() errors trigger window.onerror? )
what is the correct way to explicitly propagate it to the window error handler? this doesn't work either, for some reason it still doesn't propagate to window.error:
(async function(){
let foo= ((await ( await fetch("/no_json_plz_i_want_an_error")).json()));
})().catch(function(err){
throw err;
});
this doesn't seem to work either (why? no idea)
(async function(){
let foo= ((await ( await fetch("/no_json_plz_i_want_an_error")).json()));
})().catch(function(err){
let ev = new ErrorEvent(err.message, err);
window.dispatchEvent(ev);
});
and this approach fails with the error Uncaught (in promise) DOMException: Failed to execute 'dispatchEvent' on 'EventTarget': The event provided is uninitialized.
, whatever that means
(async function(){
let foo= ((await ( await fetch("/no_json_plz_i_want_an_error")).json()));
})().catch(function(err){
let ev= document.createEvent("ErrorEvent");
ev.error=err;
window.dispatchEvent(ev);
});
so.. what is the correct way to do it?