From 6c174f3e02334f486ec5318c2af3a417ffc1026f Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 17:03:09 -0700 Subject: [PATCH] Improve error handling for teams --- routes/manage.js | 22 ++++++++++++++-------- views/manage/addteam.pug | 1 + 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/routes/manage.js b/routes/manage.js index e16f9ff..5b979fa 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -151,19 +151,25 @@ router.post('/division', checkLoginStatus.admin, async function(req, res, next) router.get('/team', checkLoginStatus.admin, function(req, res, next) { let title = req.query.team ? 'Edit Team' : 'Add Team' - res.render('manage/addteam', { title, userLoggedIn: !!req.user }); + res.render('manage/addteam', { title, userLoggedIn: !!req.user, message: req.flash('error') }); }); -router.post('/team', checkLoginStatus.admin, function(req, res, next) { - const name = req.body['name']; - const sport = req.body['sport']; - +router.post('/team', checkLoginStatus.admin, async function(req, res, next) { const id = req.body['team']; const remove = req.body['remove']; - if(remove) teams.remove(id).then(res.redirect('/manage')); - else if(id) teams.rename(id, name).then(res.redirect('/manage')); - else teams.add(name, sport).then(res.redirect("/manage")); + try { + const name = req.body['name']; + const sport = req.body['sport']; + + if(remove) teams.remove(id).then(res.redirect('/manage')); + else if(id) teams.rename(id, name).then(res.redirect('/manage')); + else teams.add(name, sport).then(res.redirect("/manage")); + } catch(err) { + console.error("ERROR: " + err.message); + req.flash("error", "An error has occurred."); + res.redirect('/manage/team' + (id ? `?team=${id}` : '')); + } }); router.get('/account', checkLoginStatus.user, (req, res, next) => { diff --git a/views/manage/addteam.pug b/views/manage/addteam.pug index 9f7262c..78746a6 100644 --- a/views/manage/addteam.pug +++ b/views/manage/addteam.pug @@ -14,6 +14,7 @@ block content label Team 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')