2016-05-26 15:44:13 +08:00
|
|
|
///<reference path="../../typings/index.d.ts"/>
|
2016-03-26 18:19:10 +08:00
|
|
|
|
|
|
|
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
2016-05-06 16:48:51 +08:00
|
|
|
import {Error, ErrorCodes} from "../../common/entities/Error";
|
2016-03-26 18:19:10 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
export class ErrorRouter {
|
2016-03-26 18:19:10 +08:00
|
|
|
constructor(private app) {
|
|
|
|
|
2016-04-10 00:06:29 +08:00
|
|
|
this.addApiErrorHandler();
|
2016-04-22 19:23:44 +08:00
|
|
|
this.addGenericHandler();
|
2016-03-26 18:19:10 +08:00
|
|
|
}
|
|
|
|
|
2016-04-10 00:06:29 +08:00
|
|
|
private addApiErrorHandler() {
|
2016-03-26 18:19:10 +08:00
|
|
|
this.app.use("/api/*",
|
|
|
|
RenderingMWs.renderError
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-04-10 00:06:29 +08:00
|
|
|
private addGenericHandler() {
|
2016-05-09 23:04:56 +08:00
|
|
|
this.app.use((err, req, res, next) => {
|
2016-04-10 00:06:29 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
//Flush out the stack to the console
|
|
|
|
console.error(err.stack);
|
|
|
|
next(new Error(ErrorCodes.SERVER_ERROR, "Unknown server side error"));
|
|
|
|
},
|
|
|
|
RenderingMWs.renderError
|
2016-05-06 16:48:51 +08:00
|
|
|
);
|
2016-04-10 00:06:29 +08:00
|
|
|
}
|
2016-03-26 18:19:10 +08:00
|
|
|
|
|
|
|
}
|