Skip to content

Instantly share code, notes, and snippets.

@ccmelas
Created March 9, 2019 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccmelas/603ab70192b9ec1a7f5a1faeadca8074 to your computer and use it in GitHub Desktop.
Save ccmelas/603ab70192b9ec1a7f5a1faeadca8074 to your computer and use it in GitHub Desktop.
Generic error catching method for async functions. Allows you write async functions without the try/catch blocks. You instead wrap calls to those functions with the catchErrors method
/**
* Handles application errors
*/
class ErrorHandler {
/**
* Wrapper for catching async/await errors
* @param {function} fn
* @returns {function} [Composed function with error handler attached]
*/
static catchErrors(fn) {
return (req, res, next) => fn(req, res, next).catch(next);
}
}
module.exports = ErrorHandler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment