From 50b7dfaa30499510f96d550c265b2af8571f1382 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Fri, 5 Nov 2021 11:57:17 +0100 Subject: [PATCH 1/2] Added basic error handling to routex... --- libs/routex.js | 20 ++++++++++++++++---- routes/routes.js | 6 ++++-- src/sass/error.scss | 0 views/pages/error.njk | 22 ++++++++++++++++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 src/sass/error.scss create mode 100644 views/pages/error.njk diff --git a/libs/routex.js b/libs/routex.js index 1d5a333..fff35d4 100644 --- a/libs/routex.js +++ b/libs/routex.js @@ -1,10 +1,22 @@ export default function routex(req, res) { if (req.baseUrl === "/api") - return function(_, data, _2) { - res.json(data) + return function(_, data, success, __) { + if (success) + return res.json({ + success, + response: data + }) + + return res.json({ + success, + response: data.error + }) } - return function(template, data, options) { - res.render(template, { data, options }) + return function(template, data, success, options) { + if (success) + return res.render(template, { data, options }) + + return res.render("pages/error.njk", { data, options }) } } \ No newline at end of file diff --git a/routes/routes.js b/routes/routes.js index d171952..252e5f5 100644 --- a/routes/routes.js +++ b/routes/routes.js @@ -15,7 +15,7 @@ routes.get( '/maps', (req, res) => { const maps = wrapper.allMaps() - tx(req, res)('pages/maps.njk', { maps }, { currentSection: "maps" }) + tx(req, res)('pages/maps.njk', { maps }, true, { currentSection: "maps" }) } ) @@ -27,7 +27,9 @@ routes.get( const raceLeaderboard = wrapper.leaderboardRace(req.params.map, 1, 10) const teamLeaderboard = wrapper.leaderboardTeamrace(req.params.map, 1, 10) - tx(req, res)('pages/mapSingle.njk', { map, graphMap, raceLeaderboard, teamLeaderboard }, { currentSection: "maps" }) + const [success, error] = map ? [true, "No error!"] : [false, "Map not found!"] + + tx(req, res)('pages/mapSingle.njk', { map, graphMap, raceLeaderboard, teamLeaderboard, error }, success, { currentSection: "maps" }) } ) diff --git a/src/sass/error.scss b/src/sass/error.scss new file mode 100644 index 0000000..e69de29 diff --git a/views/pages/error.njk b/views/pages/error.njk new file mode 100644 index 0000000..478b992 --- /dev/null +++ b/views/pages/error.njk @@ -0,0 +1,22 @@ +{% extends "../templates/basic.njk" %} + +{% block body %} +
+
+

+ We are really sorry about this... +

+

+ There seems as if you've ran into an error... +

+

+ +
+ Reported error: +
+ + {{ data.error }} + +
+
+{% endblock %} \ No newline at end of file From 058ddfeec9ae2ed3ff34399ab04b20e295791b20 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Fri, 5 Nov 2021 12:01:36 +0100 Subject: [PATCH 2/2] :) --- src/sass/error.scss | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/sass/error.scss diff --git a/src/sass/error.scss b/src/sass/error.scss deleted file mode 100644 index e69de29..0000000