{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",
"express": "^4.18.2",
"express-session": "^1.17.3",
"http-errors": "^2.0.0",
"knacss": "^8.0.4",
"luxon": "^3.4.3",
"mongoose": "^7.5.2",

View file

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

View file

@ -17,6 +17,7 @@ import authRoutes from './routes/auth';
import {trustProxy, env, mongoDbUri, secret, port} from './config';
import {isXhr, getBaseUrl} from './helpers';
import createHttpError from 'http-errors';
passportConfig(passport);
@ -77,39 +78,23 @@ app.use('/', globalRoutes);
app.use('/', authRoutes);
// Handle 404
app.use((req, res) => {
if (isXhr(req)) {
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',
});
}
app.use((req, res, next) => {
next(createHttpError(404));
});
// Handle 500
// Handle error
app.use((error, req, res, next) => {
if (isXhr(req)) {
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 {
console.log('ERR:', error);
res.status(error.errorCode || 500);
res.status(error.errorCode || error.statusCode || 500);
res.render('index', {
pagename: '500',
viewname: 'error',
page: {
title: '500: Oups… le serveur a crashé !',
title: error.message || '500: Oups… le serveur a crashé !',
},
baseUrl: getBaseUrl(req),
params: {},