Skip to content

Instantly share code, notes, and snippets.

@jaredpalmer
Created November 30, 2016 14:44
Show Gist options
  • Save jaredpalmer/acc87ca1125867db9ac4ac9c485b5366 to your computer and use it in GitHub Desktop.
Save jaredpalmer/acc87ca1125867db9ac4ac9c485b5366 to your computer and use it in GitHub Desktop.
api error handler
return function apiErrorHandler(err, req, res, next) {
var status = err.status || err.statusCode || 500;
if (status < 400) status = 500;
res.statusCode = status;
var body = {
status: status
};
// show the stacktrace when not in production
// TODO: make this an option
if (!production) body.stack = err.stack;
// internal server errors
if (status >= 500) {
console.error(err.stack);
body.message = statuses[status];
res.json(body);
return;
}
// client errors
body.message = err.message;
if (err.code) body.code = err.code;
if (err.name) body.name = err.name;
if (err.type) body.type = err.type;
res.json(body);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment