From 3f6251f5bbbf3b1e5d3e4f80e29c5ca7c0c18cb5 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 16:38:46 -0700 Subject: [PATCH] Improve error handling for sports --- routes/manage.js | 24 ++++++++++++++++-------- views/manage/addsport.pug | 1 + 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/routes/manage.js b/routes/manage.js index d343952..4558cfd 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -90,17 +90,25 @@ router.post('/season', checkLoginStatus.admin, async function(req, res, next) { }); router.get('/sport', checkLoginStatus.admin, function(req, res, next) { - res.render('manage/addsport', { title: 'Add Sport', userLoggedIn: !!req.user }); + res.render('manage/addsport', { title: 'Add Sport', userLoggedIn: !!req.user, message: req.flash('error') }); }); -router.post('/sport', checkLoginStatus.admin, function(req, res, next) { - const name = req.body['name']; - const id = req.body['sport']; - const remove = req.body['remove']; +router.post('/sport', checkLoginStatus.admin, async function(req, res, next) { + try { + const name = req.body['name']; + const id = req.body['sport']; + const remove = req.body['remove']; - if(remove) sports.remove(id).then(res.redirect('/manage')); - else if(id) sports.rename(id, name).then(res.redirect('/manage')); - else sports.add(name).then(res.redirect('/manage')); + if(remove) await sports.remove(id); + else if(id) await sports.rename(id, name); + else await sports.add(name); + + res.redirect('/manage'); + } catch(err) { + console.error("ERROR: " + err.message); + req.flash("error", "An error has occurred."); + res.redirect('/manage/sport'); + } }); router.get('/division', checkLoginStatus.admin, function(req, res, next) { diff --git a/views/manage/addsport.pug b/views/manage/addsport.pug index 067b9ac..b4caa50 100644 --- a/views/manage/addsport.pug +++ b/views/manage/addsport.pug @@ -10,6 +10,7 @@ block content label Sport name span(class='form-section-input') input#name-textbox(type="text" name="name" disabled) + .error #{message} span(class='form-section') button#submit-button(type="submit" disabled) Submit span(class='form-section')