Improve error handling for sports

main
sudoer777 2021-11-26 16:38:46 -07:00
parent 51783c5dec
commit 3f6251f5bb
2 changed files with 17 additions and 8 deletions

View File

@ -90,17 +90,25 @@ router.post('/season', checkLoginStatus.admin, async function(req, res, next) {
}); });
router.get('/sport', checkLoginStatus.admin, 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) { router.post('/sport', checkLoginStatus.admin, async function(req, res, next) {
const name = req.body['name']; try {
const id = req.body['sport']; const name = req.body['name'];
const remove = req.body['remove']; const id = req.body['sport'];
const remove = req.body['remove'];
if(remove) sports.remove(id).then(res.redirect('/manage')); if(remove) await sports.remove(id);
else if(id) sports.rename(id, name).then(res.redirect('/manage')); else if(id) await sports.rename(id, name);
else sports.add(name).then(res.redirect('/manage')); 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) { router.get('/division', checkLoginStatus.admin, function(req, res, next) {

View File

@ -10,6 +10,7 @@ block content
label Sport name label Sport name
span(class='form-section-input') span(class='form-section-input')
input#name-textbox(type="text" name="name" disabled) input#name-textbox(type="text" name="name" disabled)
.error #{message}
span(class='form-section') span(class='form-section')
button#submit-button(type="submit" disabled) Submit button#submit-button(type="submit" disabled) Submit
span(class='form-section') span(class='form-section')