2016-03-26 11:19:10 +01:00
|
|
|
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
2016-05-06 10:48:51 +02:00
|
|
|
import {Error, ErrorCodes} from "../../common/entities/Error";
|
2017-06-04 15:25:08 +02:00
|
|
|
import {Logger} from "../Logger";
|
2016-12-26 23:36:38 +01:00
|
|
|
import Request = Express.Request;
|
|
|
|
import Response = Express.Response;
|
2016-03-26 11:19:10 +01:00
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
export class ErrorRouter {
|
2017-06-10 22:32:56 +02:00
|
|
|
public static route(app: any) {
|
2016-03-26 11:19:10 +01:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
this.addApiErrorHandler(app);
|
|
|
|
this.addGenericHandler(app);
|
|
|
|
}
|
2016-03-26 11:19:10 +01:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
private static addApiErrorHandler(app) {
|
|
|
|
app.use("/api/*",
|
|
|
|
RenderingMWs.renderError
|
|
|
|
);
|
|
|
|
};
|
2016-03-26 11:19:10 +01:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
private static addGenericHandler(app) {
|
|
|
|
app.use((err: any, req: Request, res: Response, next: Function) => {
|
2016-04-09 18:06:29 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
//Flush out the stack to the console
|
|
|
|
Logger.error(err);
|
|
|
|
next(new Error(ErrorCodes.SERVER_ERROR, "Unknown server side error"));
|
|
|
|
},
|
|
|
|
RenderingMWs.renderError
|
|
|
|
);
|
|
|
|
}
|
2016-03-26 11:19:10 +01:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|