backend: added exception handler for requests
This commit is contained in:
15
backend/src/ExceptionHandler.ts
Normal file
15
backend/src/ExceptionHandler.ts
Normal 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();
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import {MangaUpdatesCache} from './cache/MangaUpdatesCache.js';
|
||||
import * as fs from 'fs';
|
||||
import {MangaDexCache} from './cache/MangaDexCache';
|
||||
import mangaDexRouter from './router/MangaDexRouter';
|
||||
import exceptionHandler from './ExceptionHandler';
|
||||
|
||||
const config = JSON.parse(fs.readFileSync('config.json').toString());
|
||||
|
||||
@@ -26,6 +27,7 @@ app.use('/anilist', aniListRouter());
|
||||
app.use('/mangadex', mangaDexRouter(mangaDexCache));
|
||||
app.use('/mangaupdates', mangaUpdatesRouter(mangaUpdatesCache));
|
||||
app.use(express.static('_client')); //for production
|
||||
app.use(exceptionHandler());
|
||||
|
||||
//start
|
||||
app.listen(config.port, config.host, () => {
|
||||
|
||||
Reference in New Issue
Block a user