Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Top-level error handler for async functions.
- */
- async function errorWrap(cb, ...args) {
- try {
- await cb(...args);
- } catch(err) {
- if (err.stack) {
- // `Error` object
- console.log(err.stack);
- } else {
- // Other object
- console.log(err);
- }
- process.exit()
- }
- }
- // Usage
- async function foo() {
- throw new Error();
- }
- foo(); // Silently errors
- errorWrap(foo); // Logs stack-trace *and exits*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement