diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-03-20 20:23:32 +0100 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-03-20 20:23:32 +0100 |
commit | 027bfc00167705fbbaccb6510d32528114d467a7 (patch) | |
tree | 2d21c95d0f2e2a53d3ca3eee91a582e982941446 /src | |
parent | 6ebb626a4beb1d47750c944c771e4f5487fb9ce4 (diff) |
Fix server side error handeling
Diffstat (limited to 'src')
-rw-r--r-- | src/server/app.js | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/src/server/app.js b/src/server/app.js index 866e39b..4b17290 100644 --- a/src/server/app.js +++ b/src/server/app.js @@ -55,28 +55,29 @@ app.use((req, res, next) => { next(err); }); -// error handlers +function extractStatusCodeFromError(error) { + if (error.status) { + return error.status; + } else if (error.response) { + return error.response.status; + } -// development error handler -// will print stacktrace -if (app.get('env') === 'development') { - app.use((err, req, res) => { - res.status(err.status || 500); - res.render('error', { - message: err.message, - error: err, - }); - }); + return null; } -// production error handler -// no stacktraces leaked to user -app.use((err, req, res) => { - res.status(err.status || 500); +// error handler +app.use((error, req, res, next) => { + const errorCode = extractStatusCodeFromError(error) || 500; + res.status(errorCode); + res.render('error', { - message: err.message, - error: {}, + message: error.message, + error, }); + + if (error === 500) { + next(error); + } }); module.exports = app; |