node.js - How to catch an Uncaught exception in a callback in a promise -
i have big problem node.js.
for information, used version 0.12 because our app complexe , large, it's not possible migrate easilly onto newest version.
and, that, many functions callbacks functions.
but, in new part of projet want use promises easilly catch errors. , have problem uncaught exception in event loop.
this test code :
process.on('uncaughtexception', function (err) { console.error('uncaught exception'); }); new promise(function () { process.nexttick(function () { var = null; a.b; }); }).catch(function (e) { console.log('catched promise'); }); if test code, systematically pass onto uncaughtexception.
it's possible capture type of error promises ?
thanks
promises catch exceptions automatically in 2 places: 1) in promise executor function , 2) in .then() or .catch() handler. in both cases, catch exception , turn rejected promise. here couple examples:
// promise executor new promise(function(resolve, reject) { var = null; a.b = 0; // throws exception resolve("done"); }).then(function(data) { console.log(data); // never gets here }).catch(function(err) { console.log(err); // shows above exception }); // .then() handler promise.resolve().then(function(data) { var = null; a.b = 0; // throws exception return "hello"; }).then(function(data) { console.log(data); // never gets here }).catch(function(err) { console.log(err); // shows above exception }); so, if promisify async operations completion or error handlers .then() handlers or .catch() handlers, promise infrastructure catch exceptions in those. but, won't catch exceptions outside these circumstances. if you're using settimeout() or other async operation has not been wrapped in promise, have wrap callbacks in try/catch yourself. but, simple do. instead of settimeout() simple function promise based can used:
so, instead of this:
settimeout(somefunction, 100); you can use this:
delay(10).then(somefunction).catch(...); where implementation of delay() this:
function delay(t) { return new promise(function(resolve) { settimeout(resolve, t); }); } now, synchronous exceptions in somefunction() caught you. if own async operations, have converted use promises.
you can, of course, wrap own try/catch around other circumstances catch own exceptions elsewhere.
you can use bluebird's promise.promisifyall() make promisified versions of entire interface. example, use time fs module make promisified versions of entire fs interface can use promises file access functions. can see more reasons use bluebird here: are there still reasons use promise libraries q or bluebird have es6 promises?
in specific example:
new promise(function () { process.nexttick(function () { var = null; a.b; }); }).catch(function (e) { console.log('catched promise'); }); you throwing exception in async callback not 1 of conditions promises catch exceptions you. main thing can think of this:
// wrapper function run function upon nexttick , catch exceptions in function nexttick(fn) { return new promise(function(resolve, reject) { process.nexttick(function() { try { resolve(fn()); } catch(e) { reject(e); } }); }); } nexttick(function() { var = null; a.b = 0; // throw exception }).catch(function(e) { // catches exception here }); you could, of course, catch exception , handle locally, harder propagate error higher level without promises:
process.nexttick(function () { try { var = null; a.b = 0; } catch(e) { // handle exception here } });
Comments
Post a Comment