Created
June 5, 2016 10:40
-
-
Save kn9ts/70ca4ae5c192fed460667ee2f407c75a 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
// catch 404 and forward to error handler | |
app.use((req, res, next) => { | |
const err = new Error('Not Found'); | |
err.request = req.originalUrl; | |
err.status = 404; | |
next(err); | |
}); | |
// error handlers | |
app.use((err, req, res) => { | |
console.log('ERROR PASSING THROUGH', err.message); | |
// get the error stack | |
const stack = err.stack.split(/\n/) | |
.map(error => error.replace(/\s{2,}/g, ' ').trim()); | |
// send out the error as json | |
res.status(err.status || 500).json({ | |
api: err, | |
url: req.originalUrl, | |
error: err.message, | |
stack, | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment