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