Created
March 9, 2019 06:49
-
-
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
This file contains 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
/** | |
* 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