From 51783c5dec58b9f689ef33fdc365292c06a73298 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 16:34:26 -0700 Subject: [PATCH] Add better error management for seasons --- routes/manage.js | 22 +++++++++++++++------- views/manage/addseason.pug | 1 + 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/routes/manage.js b/routes/manage.js index 44abc09..d343952 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -68,17 +68,25 @@ router.post('/game', checkLoginStatus.user, async function(req, res, next) { }); router.get('/season', checkLoginStatus.admin, function(req, res, next) { - res.render('manage/addseason', { title: 'Add Season', currentYear : (new Date()).getFullYear(), userLoggedIn: !!req.user }); + res.render('manage/addseason', { title: 'Add Season', currentYear : (new Date()).getFullYear(), userLoggedIn: !!req.user, message: req.flash('error') }); }); -router.post('/season', checkLoginStatus.admin, function(req, res, next) { - const year = req.body['year']; +router.post('/season', checkLoginStatus.admin, async function(req, res, next) { + try { + const year = req.body['year']; - const seasonID = req.body['season']; - const remove = req.body['remove']; + const seasonID = req.body['season']; + const remove = req.body['remove']; - if(remove) seasons.remove(seasonID).then(res.redirect('/manage')); - else seasons.add(year).then(res.redirect("/manage")); + if(remove) await seasons.remove(seasonID); + else await seasons.add(year); + + res.redirect('/manage'); + } catch(err) { + console.error("ERROR: " + err.message); + req.flash("error", "An error has occurred."); + res.redirect('/manage/season'); + } }); router.get('/sport', checkLoginStatus.admin, function(req, res, next) { diff --git a/views/manage/addseason.pug b/views/manage/addseason.pug index 27783b5..ba43698 100644 --- a/views/manage/addseason.pug +++ b/views/manage/addseason.pug @@ -10,6 +10,7 @@ block content label Ending year span(class='form-section-input') input#season-textbox(type="number", name="year", value=currentYear) + .error #{message} span(class='form-section') button#submit-button(type="submit" disabled) Submit