Consider some long stack trace module. It can use async_wrap to manage the the callSite objects correctly and use the v8 Error hooks to modify the .stack property. However because v8 sets the .stack property in a lazy way, it is necessary to do a try {} finally {} around the callback.
See https://github.com/AndreasMadsen/trace/blob/master/trace.js#L53 for an example with the tracing module.
An okay solution solution is to use the uncaughtException event like this:
varasyncWrap=process.binding('async_wrap');// Enable asyncWrap and call init hook function on async initializationvarasyncHooksObject={};varkCallInitHook=0;asyncWrap.setupHooks(asyncHooksObject,asyncFunctionInitialized,asyncCallbackBefore,asyncCallbackAfter);asyncHooksObject[kCallInitHook]=1;functionasyncFunctionInitialized(){}functionasyncCallbackBefore(){process.once('uncaughtException',asyncCallbackError);}functionasyncCallbackError(error){// Set stack by v8 magicerror.stack;// changes throw origin, should not be necessarythrowerror;}functionasyncCallbackAfter(){process.removeListener('uncaughtException',asyncCallbackError);}setTimeout(function(){badluck();},10);However this changes the throw origin:
/Users/Andreas/Sites/node_modules/trace/test.js:26
throw error;
^
ReferenceError: badluck is not defined
at null._onTimeout (/Users/Andreas/Sites/node_modules/trace/test.js:34:3)
at Timer.listOnTimeout (timers.js:90:15)
If uncaughtException isn't used then this is the error:
/Users/Andreas/Sites/node_modules/trace/test.js:34
badluck();
^
ReferenceError: badluck is not defined
at null._onTimeout (/Users/Andreas/Sites/node_modules/trace/test.js:34:3)
at Timer.listOnTimeout (timers.js:90:15)
This is much more informative. It would be really nice if async_wrap or some other mechanism allowed something similar to the old tracing.addAsyncListener({ error: handler }) behaviour. Such that the throw origin can be preserved.
issue tracking: AndreasMadsen/trace#12
issue tracking: nodejs/diagnostics#7
Consider some long stack trace module. It can use
async_wrapto manage the the callSite objects correctly and use the v8Errorhooks to modify the.stackproperty. However because v8 sets the.stackproperty in a lazy way, it is necessary to do atry {} finally {}around the callback.See https://github.com/AndreasMadsen/trace/blob/master/trace.js#L53 for an example with the
tracingmodule.An okay solution solution is to use the
uncaughtExceptionevent like this:However this changes the throw origin:
If
uncaughtExceptionisn't used then this is the error:This is much more informative. It would be really nice if
async_wrapor some other mechanism allowed something similar to the oldtracing.addAsyncListener({ error: handler })behaviour. Such that the throw origin can be preserved.issue tracking: AndreasMadsen/trace#12
issue tracking: nodejs/diagnostics#7