add 500 error handler
This commit is contained in:
parent
e69e49fae2
commit
4ae9fa9ead
19
index.js
19
index.js
|
@ -16,6 +16,7 @@ let RedisStore = require("connect-redis")(session)
|
|||
|
||||
const { APP_PORT, APP_KEY } = process.env;
|
||||
|
||||
|
||||
app.use(bodyparser.json());
|
||||
app.use(bodyparser.urlencoded({ extended: true }));
|
||||
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, () => {
|
||||
console.log("Listening on port " + APP_PORT);
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue