Skip to content

Instantly share code, notes, and snippets.

@thomasboyt
Last active November 21, 2015 10:51
Show Gist options
  • Save thomasboyt/56ae303b4749727d1042 to your computer and use it in GitHub Desktop.
Save thomasboyt/56ae303b4749727d1042 to your computer and use it in GitHub Desktop.
/*
* 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