1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00
pigallery2/backend/routes/ErrorRouter.ts

33 lines
899 B
TypeScript
Raw Normal View History

2018-03-30 15:30:30 -04:00
import {RenderingMWs} from '../middlewares/RenderingMWs';
import {ErrorCodes, ErrorDTO} from '../../common/entities/Error';
import {Logger} from '../Logger';
2016-12-26 23:36:38 +01:00
import Request = Express.Request;
import Response = Express.Response;
2018-11-28 23:49:33 +01:00
import {Express} from 'express';
2016-05-09 17:04:56 +02:00
export class ErrorRouter {
2018-11-28 23:49:33 +01:00
public static route(app: Express) {
this.addApiErrorHandler(app);
this.addGenericHandler(app);
}
2018-11-28 23:49:33 +01:00
private static addApiErrorHandler(app: Express) {
2018-03-30 15:30:30 -04:00
app.use('/api/*',
RenderingMWs.renderError
);
2018-03-30 15:30:30 -04:00
}
2018-11-28 23:49:33 +01:00
private static addGenericHandler(app: Express) {
app.use((err: any, req: Request, res: Response, next: Function) => {
2018-03-30 15:30:30 -04:00
// Flush out the stack to the console
Logger.error('Unexpected error:');
2017-07-17 23:12:12 +02:00
console.error(err);
2018-03-30 15:30:30 -04:00
next(new ErrorDTO(ErrorCodes.SERVER_ERROR, 'Unknown server side error', err));
},
RenderingMWs.renderError
);
}
}