1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/routes/ErrorRouter.ts
2018-11-28 23:49:33 +01:00

33 lines
899 B
TypeScript

import {RenderingMWs} from '../middlewares/RenderingMWs';
import {ErrorCodes, ErrorDTO} from '../../common/entities/Error';
import {Logger} from '../Logger';
import Request = Express.Request;
import Response = Express.Response;
import {Express} from 'express';
export class ErrorRouter {
public static route(app: Express) {
this.addApiErrorHandler(app);
this.addGenericHandler(app);
}
private static addApiErrorHandler(app: Express) {
app.use('/api/*',
RenderingMWs.renderError
);
}
private static addGenericHandler(app: Express) {
app.use((err: any, req: Request, res: Response, next: Function) => {
// Flush out the stack to the console
Logger.error('Unexpected error:');
console.error(err);
next(new ErrorDTO(ErrorCodes.SERVER_ERROR, 'Unknown server side error', err));
},
RenderingMWs.renderError
);
}
}