javascript - How can I trigger global onError handler via native Promise when runtime error occurs? -
this question has answer here:
with q.js can trigger window.onerror using .done():
window.onerror = function() { console.log('error handler'); } var q = q('initvalue').then(function (data) { throw new error("can't , use promise.catch method."); }); q.catch(function(error){ throw new error("this last exception trigger window.onerror handler via done method."); }) .done();
in native promise (es6) have no .done(), , last ".catch" end of chain:
var p = new promise(function () { throw new error("fail"); }); p.catch(function (error) { throw new error("last exception in last chain link"); });
"throw new error(...)" in ".catch" 1 of simplest way reproduce runtime error. in reality, may analog of runtime error (evalerror, syntaxerror, typeerror, etc.), e.g.:
var = []; a(); // uncaught typeerror: not function
.done usage example explain target more detail. haven't goal duplicate .done api.
my task is: have promises chain , handler on window.onerror. errors inside chain can handle .cath, except runtime error @ end of chain. when runtime exception occurs @ end of promise's methods chain, need have triggered handler hanged on window.onerror.
the restrictions: native js, must use window.onerror.
what best way trigger global handler via native promise?
throw error asynchronously:
window.addeventlistener('error', function() { console.log('error handler'); }); new promise(function () { throw new error("fail"); }).catch(function (error) { settimeout(() => {throw new error("last exception in last chain link")}, 0); });
Comments
Post a Comment