{BUGFIX} 404 error

This commit is contained in:
Damien Broqua 2023-12-22 09:10:18 +01:00
parent 95481a07fa
commit 17f7d939ea
3 changed files with 9 additions and 22 deletions

1
package-lock.json generated
View file

@ -18,6 +18,7 @@
"ejs": "^3.1.9", "ejs": "^3.1.9",
"express": "^4.18.2", "express": "^4.18.2",
"express-session": "^1.17.3", "express-session": "^1.17.3",
"http-errors": "^2.0.0",
"knacss": "^8.0.4", "knacss": "^8.0.4",
"luxon": "^3.4.3", "luxon": "^3.4.3",
"mongoose": "^7.5.2", "mongoose": "^7.5.2",

View file

@ -58,6 +58,7 @@
"ejs": "^3.1.9", "ejs": "^3.1.9",
"express": "^4.18.2", "express": "^4.18.2",
"express-session": "^1.17.3", "express-session": "^1.17.3",
"http-errors": "^2.0.0",
"knacss": "^8.0.4", "knacss": "^8.0.4",
"luxon": "^3.4.3", "luxon": "^3.4.3",
"mongoose": "^7.5.2", "mongoose": "^7.5.2",

View file

@ -17,6 +17,7 @@ import authRoutes from './routes/auth';
import {trustProxy, env, mongoDbUri, secret, port} from './config'; import {trustProxy, env, mongoDbUri, secret, port} from './config';
import {isXhr, getBaseUrl} from './helpers'; import {isXhr, getBaseUrl} from './helpers';
import createHttpError from 'http-errors';
passportConfig(passport); passportConfig(passport);
@ -77,39 +78,23 @@ app.use('/', globalRoutes);
app.use('/', authRoutes); app.use('/', authRoutes);
// Handle 404 // Handle 404
app.use((req, res) => { app.use((req, res, next) => {
if (isXhr(req)) { next(createHttpError(404));
res.status(404).send({message: '404: Not found'});
} else {
res.status(404);
res.render('index', {
pagename: '404',
viewname: 'error',
page: {
title: '404: Cette page n\'existe pas.',
},
baseUrl: getBaseUrl(req),
params: {},
query: {},
user: req.user,
error: 'La page demandée n\'existe pas sur le server',
});
}
}); });
// Handle 500 // Handle error
app.use((error, req, res, next) => { app.use((error, req, res, next) => {
if (isXhr(req)) { if (isXhr(req)) {
const {message, errorCode, date} = error; const {message, errorCode, date} = error;
res.status(error.errorCode || 500).send({message, errorCode, date}); res.status(error.errorCode || error.statusCode || 500).send({message, errorCode, date});
} else { } else {
console.log('ERR:', error); console.log('ERR:', error);
res.status(error.errorCode || 500); res.status(error.errorCode || error.statusCode || 500);
res.render('index', { res.render('index', {
pagename: '500', pagename: '500',
viewname: 'error', viewname: 'error',
page: { page: {
title: '500: Oups… le serveur a crashé !', title: error.message || '500: Oups… le serveur a crashé !',
}, },
baseUrl: getBaseUrl(req), baseUrl: getBaseUrl(req),
params: {}, params: {},