Rework error message render
This commit is contained in:
parent
0b20b10f5f
commit
3c5185496f
9 changed files with 107 additions and 118 deletions
10
app.js
10
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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
4
routes/utils/error.js
Normal file
4
routes/utils/error.js
Normal file
|
@ -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}});
|
||||
};
|
Reference in a new issue