Last active
November 21, 2015 10:51
-
-
Save thomasboyt/56ae303b4749727d1042 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment