Created
January 11, 2023 14:36
-
-
Save jhaashutosh/509ad31461bb5b8ca31121fe80c66d66 to your computer and use it in GitHub Desktop.
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
const notFoundHandler = (req, res, next) => { | |
const error = new Error(`Not found - ${req.originalUrl}`); | |
res.status(404); | |
next(error); | |
}; | |
const errorHandler = (err, req, res, next) => { | |
const statusCode = res.statusCode === 200 ? 500 : res.statusCode; //we sometimes get 200 code (ok) even if its error. 500 means server error | |
res.status(statusCode); | |
res.json({ | |
message: err.message, | |
stack: process.env.NODE_ENV === "production" ? null : err.stack, | |
}); | |
}; | |
module.exports = { notFoundHandler, errorHandler }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment