Created
December 15, 2016 18:01
-
-
Save davidmfoley/e30fcceedc6b2b64c62a9112829ee7b1 to your computer and use it in GitHub Desktop.
Example express error handler middleware snippet
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
res.apiRespond = function(err, data) { | |
if (err) { | |
var code = (err.httpStatusCode || 500); | |
if (code === 500) { | |
logger.error('API error: ' + req.method + ' ' + req.originalUrl, err); | |
} | |
res.status(code).send(formatter.format(err)); | |
} else if (!data && req.method === 'GET') { | |
res.notFound(); | |
} else { | |
if (req.method === 'POST') { | |
res.status(201); | |
} | |
sendResponse(req, res, data); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment