Lightweight Asynchronous Error Handling
callAsyncFunctionarg arg// err is not checked but should be (a common case)throw 'fail'; // uncaught - will exit Node.js
callAsyncFunctionarg argiferrcallbackerr;elsetrythrow 'fail';catchecallbacke; // caught - return control manually
callAsyncFunctionarg arg _xthrow 'fail';callback // in case of error return control to callbacktrue; // automatically check the err parameter
LAEH stores the stacktrace of the thread that initiated the asynchronous operation which in turn called the callback. This stacktrace is then appended to the primary stacktrace of the error which happened in the callback, or the error which was passed to the callback by the asynchronous function.
LAEH then presents the stacktrace in a minified format, with optional hiding of frames of the laeh.js itself, of the node.js core library, shortens the often repeating string /node_modules/ into /$/, and removes the current directory path prefix from the file names in the stacktrace.
npm install laeh
var laeh = require'laeh'leanStackstrue '\t';var _e = laeh_e;var _x = laeh_x;
leanStacks(hiding, prettyMeta) call is optional, the
hiding will hide stack frames from Node's core .js files and from
laeh.js itself. The
prettyMeta is the third parameter for the
JSON.stringify function, which is used to serialize your metadata objects (see below), and leaving it empty will serialize your metadata objects in-line.
_e(err, meta) function is just a convenient error checking, wrapping and throwing. E.g.
_e('something') will throw
new Error('something') and
_e(null) will not do anything. The
meta parameter is an optional accompanying information for the error to be thrown, which is then displayed when you let LAEH to display your errors using the
_x(func, cb, chk), the func is you callback to be wrapped. If it follows the node convention of
func(err, args), you can pass
chk as true, which will automatically check for the
err to be null, and call the eventual callback if it isn't null. The eventual callback is passed as the cb argument, or if omitted, it is tried to be derived from the last argument parseed to the function you are wrapping, e.g. if the signature is
func(err, args, cb), the cb is taken.