Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. /*
  2. * Top-level error handler for async functions.
  3. */
  4. async function errorWrap(cb, ...args) {
  5. try {
  6. await cb(...args);
  7. } catch(err) {
  8. if (err.stack) {
  9. // `Error` object
  10. console.log(err.stack);
  11. } else {
  12. // Other object
  13. console.log(err);
  14. }
  15.  
  16. process.exit()
  17. }
  18. }
  19.  
  20. // Usage
  21. async function foo() {
  22. throw new Error();
  23. }
  24.  
  25. foo(); // Silently errors
  26. errorWrap(foo); // Logs stack-trace *and exits*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement