add 500 error handler

This commit is contained in:
b1ek 2023-02-19 16:54:38 +10:00
parent e69e49fae2
commit 4ae9fa9ead
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 34 additions and 0 deletions

View File

@ -16,6 +16,7 @@ let RedisStore = require("connect-redis")(session)
const { APP_PORT, APP_KEY } = process.env; const { APP_PORT, APP_KEY } = process.env;
app.use(bodyparser.json()); app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true })); app.use(bodyparser.urlencoded({ extended: true }));
app.use(cookie_parse(APP_KEY)) app.use(cookie_parse(APP_KEY))
@ -37,6 +38,24 @@ app.use((req, res, next) => {
}) })
// error handler
app.use(async (err, req, res, next) => {
console.log(err);
if (res.headersSent) {
return next(err);
}
const Helpers = require('./helpers');
res.status(500);
res.send(await Helpers.ViewLoader.load('error.pug', {
error: '500 Internal Server Error',
message: 'An unexpected error happened in the server'
}));
})
const server = app.listen(APP_PORT, () => { const server = app.listen(APP_PORT, () => {
console.log("Listening on port " + APP_PORT); console.log("Listening on port " + APP_PORT);
}); });

View File

@ -0,0 +1,15 @@
const Helpers = require('../helpers');
async function handler(err, req, res, next) {
if (res.headersSent) {
return next(err)
}
res.status(500).send(Helpers.ViewLoader.load('error.pug', {
error: '500 Internal Server Error',
message: 'An unexpected error happened in the server'
}));
}
module.exports = (router) => {
// router.use(handler);
}