Created
November 30, 2016 14:44
-
-
Save jaredpalmer/acc87ca1125867db9ac4ac9c485b5366 to your computer and use it in GitHub Desktop.
api error handler
This file contains hidden or 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
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