backend: added exception handler for requests

This commit is contained in:
wea_ondara
2023-11-25 17:14:07 +01:00
parent c5cd379b8a
commit 0205c4eda5
2 changed files with 17 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
import {ErrorRequestHandler, NextFunction, Request, Response} from 'express';
export default function exceptionHandler(): ErrorRequestHandler {
return (err: any, req: Request, res: Response, next: NextFunction) => {
try {
console.error(err);
res.status(500).send('Internal server error: ' + err.message);
} catch (ex) {
console.error('Error in exception handler!');
console.error(ex);
} finally {
next();
}
};
}

View File

@@ -7,6 +7,7 @@ import {MangaUpdatesCache} from './cache/MangaUpdatesCache.js';
import * as fs from 'fs'; import * as fs from 'fs';
import {MangaDexCache} from './cache/MangaDexCache'; import {MangaDexCache} from './cache/MangaDexCache';
import mangaDexRouter from './router/MangaDexRouter'; import mangaDexRouter from './router/MangaDexRouter';
import exceptionHandler from './ExceptionHandler';
const config = JSON.parse(fs.readFileSync('config.json').toString()); const config = JSON.parse(fs.readFileSync('config.json').toString());
@@ -26,6 +27,7 @@ app.use('/anilist', aniListRouter());
app.use('/mangadex', mangaDexRouter(mangaDexCache)); app.use('/mangadex', mangaDexRouter(mangaDexCache));
app.use('/mangaupdates', mangaUpdatesRouter(mangaUpdatesCache)); app.use('/mangaupdates', mangaUpdatesRouter(mangaUpdatesCache));
app.use(express.static('_client')); //for production app.use(express.static('_client')); //for production
app.use(exceptionHandler());
//start //start
app.listen(config.port, config.host, () => { app.listen(config.port, config.host, () => {