Skip to content

Instantly share code, notes, and snippets.

@kn9ts
Created June 5, 2016 10:40
Show Gist options
  • Save kn9ts/70ca4ae5c192fed460667ee2f407c75a to your computer and use it in GitHub Desktop.
Save kn9ts/70ca4ae5c192fed460667ee2f407c75a to your computer and use it in GitHub Desktop.
// 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