From 3c5185496f37cccf8b5b7105f329185a0407149a Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 20 Aug 2020 12:14:38 +0200 Subject: [PATCH] Rework error message render --- app.js | 10 ++--- middlewares/sessionCheck.js | 10 ++--- routes/admin/orders.js | 11 +++-- routes/admin/sandwiches/edit.js | 52 ++++++++++------------ routes/admin/sandwiches/index.js | 19 ++++---- routes/order.js | 31 +++++-------- routes/profile.js | 13 +++--- routes/register.js | 75 ++++++++++++++++---------------- routes/utils/error.js | 4 ++ 9 files changed, 107 insertions(+), 118 deletions(-) create mode 100644 routes/utils/error.js diff --git a/app.js b/app.js index 8850074..afac2d8 100644 --- a/app.js +++ b/app.js @@ -63,13 +63,13 @@ app.use((req, res) => { // error handler app.use((err, req, res) => { - // set locals, only providing error in development - res.locals.message = err.message; - res.locals.error = req.app.get("env") === "development" ? err : {}; - // render the error page res.status(err.status || 500); - res.render("error"); + res.render("error", { + user: req.session.user, + message: err.message, + error: req.app.get("env") === "development" ? err : {} + }); }); module.exports = app; diff --git a/middlewares/sessionCheck.js b/middlewares/sessionCheck.js index d579232..91551c7 100644 --- a/middlewares/sessionCheck.js +++ b/middlewares/sessionCheck.js @@ -1,13 +1,13 @@ +let error = require("../routes/utils/error"); + function sessionCheck(permission) { return (req, res, next) => { if (!req.session.user) { req.session.lastUrl = req.originalUrl; req.session.save(() => res.redirect("/login")); - } else if (req.session.user.permissions < permission) { - res.status(403); - res.render("error", {message: "Permission denied !", "error": {}}); - } else - next(); + } else if (req.session.user.permissions < permission) + return error(req, res, "Permission denied !", 403); + next(); } } diff --git a/routes/admin/orders.js b/routes/admin/orders.js index 5b55054..cd9c0e7 100644 --- a/routes/admin/orders.js +++ b/routes/admin/orders.js @@ -2,6 +2,7 @@ let express = require("express"); let router = express.Router(); let sessionCheck = require("../../middlewares/sessionCheck"); let models = require("../../models"); +let error = require("../utils/error"); router.get("/", sessionCheck(3), async (req, res) => { @@ -12,17 +13,19 @@ router.get("/", sessionCheck(3), async (req, res) => { }); }).post("/order/delete", sessionCheck(3), async (req, res) => { if (!req.body.id) - res.render("error", {message: "Fail to remove order !", error: {status: "Missing args"}}); + return error(req, res, "Fail to remove order !", 400, "Missing args"); + try { await (await models.Order.findByPk(req.body.id)).destroy(); res.redirect("/admin/orders"); } catch (e) { - res.render("error", {message: "Fail to remove order !", error: {}}); + error(req, res, "Fail to remove order !"); throw e; } }).post("/sandwich/delete", sessionCheck(3), async (req, res) => { if (!req.body.id) - res.render("error", {message: "Fail to remove sandwich !", error: {status: "Missing args"}}); + return error(req, res, "Fail to remove sandwich !", 400, "Missing args"); + try { let sandwich = await models.SandwichOrder.findByPk(req.body.id); let order = await models.Order.findByPk(sandwich.OrderId, {include: models.Sandwich}); @@ -32,7 +35,7 @@ router.get("/", sessionCheck(3), async (req, res) => { await order.destroy(); res.redirect("/admin/orders"); } catch (e) { - res.render("error", {message: "Fail to remove sandwich !", error: {}}); + error(req, res, "Fail to remove sandwich !"); throw e; } }); diff --git a/routes/admin/sandwiches/edit.js b/routes/admin/sandwiches/edit.js index b648cc0..0ae55fb 100644 --- a/routes/admin/sandwiches/edit.js +++ b/routes/admin/sandwiches/edit.js @@ -2,39 +2,35 @@ let express = require("express"); let router = express.Router(); let sessionCheck = require("../../../middlewares/sessionCheck"); let models = require("../../../models"); +let error = require("../../utils/error"); router.get("/", sessionCheck(3), async (req, res) => { if (!req.query.name) - res.render("error", {message: "Can't edit sandwich !", error: {status: "Missing arg"}}); - else { - let sandwich = await models.Sandwich.findByPk(req.query.name); - if (!sandwich) - res.render("error", - {message: "Can't edit sandwich !", error: {status: "Sandwich not found"}}); - else - res.render("admin/sandwiches/edit", { - title: "SOD - Sandwiches administration", - user: req.session.user, - sandwich: sandwich - }); - } + return error(req, res, "Can't edit sandwich !", 400, "Missing arg"); + + let sandwich = await models.Sandwich.findByPk(req.query.name); + if (!sandwich) + return error(req, res, "Can't edit sandwich !", 400, "Sandwich not found"); + + res.render("admin/sandwiches/edit", { + title: "SOD - Sandwiches administration", + user: req.session.user, + sandwich: sandwich + }); }).post("/", sessionCheck(3), async (req, res) => { if (!req.body.name) - res.render("error", {message: "Fail to edit sandwich !", error: {status: "Missing arg"}}); - else { - let sandwich = await models.Sandwich.findByPk(req.body.name); - if (!sandwich) - res.render("error", - {message: "Fail to edit sandwich !", error: {status: "Invalid sandwich name"}}); - else { - if (req.body.price && req.body.price !== sandwich.price) - sandwich.price = req.body.price; - await sandwich.save(); - if (req.body.newName && req.body.newName !== sandwich.name) - await models.Sandwich.update({name: req.body.newName}, {where: {name: req.body.name}}); - res.redirect("/admin/sandwiches"); - } - } + return error(req, res, "Fail to edit sandwich !", 400, "Missing arg"); + + let sandwich = await models.Sandwich.findByPk(req.body.name); + if (!sandwich) + return error(req, res, "Fail to edit sandwich !", 400, "Invalid sandwich name"); + + if (req.body.price && req.body.price !== sandwich.price) + sandwich.price = req.body.price; + await sandwich.save(); + if (req.body.newName && req.body.newName !== sandwich.name) + await models.Sandwich.update({name: req.body.newName}, {where: {name: req.body.name}}); + res.redirect("/admin/sandwiches"); }); module.exports = router; diff --git a/routes/admin/sandwiches/index.js b/routes/admin/sandwiches/index.js index 36fe5f1..3ead76a 100644 --- a/routes/admin/sandwiches/index.js +++ b/routes/admin/sandwiches/index.js @@ -2,6 +2,7 @@ let express = require("express"); let router = express.Router(); let sessionCheck = require("../../../middlewares/sessionCheck"); let models = require("../../../models"); +let error = require("../../utils/error"); router.get("/", sessionCheck(3), async (req, res) => { @@ -14,16 +15,14 @@ router.get("/", sessionCheck(3), async (req, res) => { .use("/edit", require("./edit")) .get("/delete", sessionCheck(3), async (req, res) => { if (!req.query.name) - res.render("error", {message: "Can't remove sandwich !", error: {status: "Missing arg"}}); - else { - let sandwich = await models.Sandwich.findByPk(req.query.name); - if (!sandwich) - res.render("error", {message: "Can't remove sandwich !", error: {status: "Invalid sandwich"}}); - else { - await sandwich.destroy(); - res.redirect("/admin/sandwiches"); - } - } + return error(req, res, "Can't remove sandwich !", 400, "Missing arg"); + + let sandwich = await models.Sandwich.findByPk(req.query.name); + if (!sandwich) + return error(req, res, "Can't remove sandwich !", 400, "Invalid sandwich"); + + await sandwich.destroy(); + res.redirect("/admin/sandwiches"); }); module.exports = router; diff --git a/routes/order.js b/routes/order.js index e016163..0167540 100644 --- a/routes/order.js +++ b/routes/order.js @@ -1,36 +1,27 @@ let express = require("express"); let router = express.Router(); let models = require("../models"); +let error = require("./utils/error"); router.post("/", async (req, res) => { - if (!req.body.department || !req.body.firstName || !req.body.lastName || !req.body.sandwich1 || !req.body.date1) { - res.render("error", {message: "Invalid order !", "error": {status: "Missing arguments"}}); - return; - } + if (!req.body.department || !req.body.firstName || !req.body.lastName || !req.body.sandwich1 || !req.body.date1) + return error(req, res, "Invalid order !", 400, "Missing arguments"); let department = await models.Department.findByPk(req.body.department); - if (!department) { - res.render("error", {message: "Invalid order !", error: {status: "Invalid department"}}); - return; - } + if (!department) + return error(req, res, "Invalid order !", 400, "Invalid department"); let sandwiches = []; let price = 0; for (let i = 1; req.body["sandwich" + i] !== undefined; i++) { - if (req.body["date" + i] === undefined) { - res.render("error", {message: "Invalid order !", error: {status: "Sandwich without date"}}); - return; - } + if (req.body["date" + i] === undefined) + return error(req, res, "Invalid order !", 400, "Sandwich without date"); let sandwich = await models.Sandwich.findByPk(req.body["sandwich" + i]); - if (!sandwich) { - res.render("error", { - message: "Invalid order !", - error: {status: "Invalid sandwich: "+req.body["sandwich" + i]} - }); - return; - } + if (!sandwich) + return error(req, res, "Invalid order !", 400, "Invalid sandwich: "+req.body["sandwich" + i]); + sandwiches.push([sandwich.name, req.body["date" + i]]); price += sandwich.price; } @@ -49,7 +40,7 @@ router.post("/", async (req, res) => { await models.SandwichOrder.create({OrderId: order.id, SandwichName: data[0], date: data[1]}); } catch (e) { await order.destroy(); - res.render("error", {message: "Invalid order !", error: {status: "Invalid date"}}); + error(req, res, "Invalid order !", 400, "Invalid date"); throw e; } res.send("Ok"); diff --git a/routes/profile.js b/routes/profile.js index 9a28373..5e3a48e 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -2,6 +2,7 @@ let express = require("express"); let router = express.Router(); let sessionCheck = require("../middlewares/sessionCheck"); let models = require("../models"); +let error = require("./utils/error"); router.get("/", sessionCheck(0), async (req, res) => { res.render("profile", { @@ -19,23 +20,20 @@ router.get("/", sessionCheck(0), async (req, res) => { /*if (req.body.username && req.body.username !== user.username) if (await models.User.findByPk(req.body.username)) - res.render("error", {message: "Invalid profile update !", - error: {status: "Username already taken"}}); + return error(req, res, "Invalid profile update !", 400, "Username already taken"); else user.username = req.body.username;*/ if (req.body.email && req.body.email !== user.email) if (await models.User.findOne({where: {email: req.body.email}})) - res.render("error", {message: "Invalid profile update !", - error: {status: "Email already used"}}); + return error(req, res, "Invalid profile update !", 400, "Email already used"); else user.email = req.body.email; if (req.body.firstName && req.body.lastName && (req.body.firstName !== user.firstName || req.body.lastName !== user.lastName)) if (await models.User.findOne({where: {firstName: req.body.firstName, lastName: req.body.lastName}})) - res.render("error", {message: "Invalid profile update !", - error: {status: "First & last name already register"}}); + return error(req, res, "Invalid profile update !", 400, "First & last name already register"); else { user.firstName = req.body.firstName; user.lastName = req.body.lastName; @@ -50,8 +48,7 @@ router.get("/", sessionCheck(0), async (req, res) => { if (req.body.department && req.body.department !== user.DepartmentName) if (!await models.Department.findByPk(req.body.department)) - res.render("error", {message: "Invalid profile update !", - error: {status: "Invalid department"}}); + return error(req, res, "Invalid profile update !", 400, "Invalid department"); else user.DepartmentName = req.body.department; diff --git a/routes/register.js b/routes/register.js index 807c834..3dae715 100644 --- a/routes/register.js +++ b/routes/register.js @@ -1,6 +1,7 @@ let express = require("express"); let router = express.Router(); let models = require("../models"); +let error = require("./utils/error"); router.get("/", async (req, res) => { @@ -12,45 +13,43 @@ router.get("/", async (req, res) => { .post("/", async (req, res) => { if (!req.body.username || !req.body.email || !req.body.firstName || !req.body.lastName || !req.body.department || !req.body.password) - res.render("error", {message: "Invalid register !", error: {status: "Missing args"}}); - else if (await models.User.findByPk(req.body.username)) - res.render("error", {message: "Invalid register !", - error: {status: "Username already taken"}}); - else if (await models.User.findOne({where: {firstName: req.body.firstName, - lastName: req.body.lastName}})) - res.render("error", {message: "Invalid register !", - error: {status: "First & last name already register"}}); - else if (await models.User.findOne({where: {email: req.body.email}})) - res.render("error", {message: "Invalid register !", error: {status: "Email already used"}}); - else { - let department = await models.Department.findByPk(req.body.department); - if (!department) - res.render("error", {message: "Invalid register !", - error: {status: "Invalid department"}}); - else { - try { - let user = await models.User.create({ - username: req.body.username, - email: req.body.email, - firstName: req.body.firstName, - lastName: req.body.lastName, - passwordHash: req.body.password - }); + return error(req, res, "", 400, "Missing args"); - await user.setDepartment(department); - req.session.user = user; - res.redirect("/"); - for (let c of await models.Order.findAll({where: { - firstName: user.firstName, - lastName: user.lastName, - UserUsername: null - }})) - await c.setUser(user); - } catch (e) { - res.render("error", {message: "Registration fail !", error: {}}); - throw e; - } - } + if (await models.User.findByPk(req.body.username)) + return error(req, res, "Invalid register !", 400, "Username already taken"); + + if (await models.User.findOne({where: {firstName: req.body.firstName, + lastName: req.body.lastName}})) + return error(req, res, "Invalid register !", 400, "First & last name already register"); + + if (await models.User.findOne({where: {email: req.body.email}})) + return error(req, res, "Invalid register !", 400, "Email already used"); + + let department = await models.Department.findByPk(req.body.department); + if (!department) + return error(req, res, "Invalid register !", 400, "Invalid department"); + + try { + let user = await models.User.create({ + username: req.body.username, + email: req.body.email, + firstName: req.body.firstName, + lastName: req.body.lastName, + passwordHash: req.body.password + }); + + await user.setDepartment(department); + req.session.user = user; + res.redirect("/"); + for (let c of await models.Order.findAll({where: { + firstName: user.firstName, + lastName: user.lastName, + UserUsername: null + }})) + await c.setUser(user); + } catch (e) { + error(req, res, "Registration fail !"); + throw e; } }); diff --git a/routes/utils/error.js b/routes/utils/error.js new file mode 100644 index 0000000..ab48b38 --- /dev/null +++ b/routes/utils/error.js @@ -0,0 +1,4 @@ +module.exports = (req, res, message, status, subMessage) => { + res.status(status || 500); + res.render("error", {user: req.session.user, message: message, error: {status: subMessage || undefined}}); +};