aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2018-03-20 22:35:36 +0100
committerNoah Loomans <noahloomans@gmail.com>2018-03-20 22:35:36 +0100
commit72ac0866d9ddccae43de8d5a19e8a9a01b131e9f (patch)
tree641ff370e8dd066253d71af9b0c3f58562cedb9a
parent29f7292f84d456a49dd8e2dcd93ab2395470aef0 (diff)
Refactor error handling code
-rw-r--r--src/server/app.js14
-rw-r--r--src/server/routes/getSchedule.js4
2 files changed, 5 insertions, 13 deletions
diff --git a/src/server/app.js b/src/server/app.js
index 7e8d9e9..bd87162 100644
--- a/src/server/app.js
+++ b/src/server/app.js
@@ -54,21 +54,9 @@ app.use((req, res, next) => {
next(err);
});
-function extractStatusCodeFromError(error) {
- if (error.status) {
- return error.status;
- } else if (error.response) {
- return error.response.status;
- }
-
- return null;
-}
-
// error handler
app.use((error, req, res, next) => {
- const errorCode = extractStatusCodeFromError(error) || 500;
- res.status(errorCode);
-
+ res.status(error.status || 500);
res.render('error', {
message: error.message,
error,
diff --git a/src/server/routes/getSchedule.js b/src/server/routes/getSchedule.js
index f1120b7..04df28a 100644
--- a/src/server/routes/getSchedule.js
+++ b/src/server/routes/getSchedule.js
@@ -69,6 +69,10 @@ router.get('/:type/:value', (req, res, next) => {
res.status(response.status).end(response.data);
})
.catch((err) => {
+ if (err.response) {
+ // eslint-disable-next-line no-param-reassign
+ err.status = err.response.status;
+ }
next(err);
});
});