Browse Source

fix: Error handling

update-deps
Alex Mikhalev 6 years ago
parent
commit
4963e31473
  1. 1
      common/ApiError.ts
  2. 6
      server/express/errorHandler.ts

1
common/ApiError.ts

@ -25,6 +25,7 @@ export default class ApiError extends Error { @@ -25,6 +25,7 @@ export default class ApiError extends Error {
toJSON(development: boolean = false) {
return {
message: this.message,
statusCode: this.statusCode,
code: this.code,
data: development ? this.data : undefined
};

6
server/express/errorHandler.ts

@ -14,13 +14,15 @@ const errorHandler: express.ErrorRequestHandler = ( @@ -14,13 +14,15 @@ const errorHandler: express.ErrorRequestHandler = (
if (err instanceof ApiError) {
// TODO: different content-type?
res.status(err.statusCode).json(err.toJSON(isDev));
} else {
} else if (err) {
const internalError = new ApiError(
"An internal server error has occurred",
ErrorCode.Internal
ErrorCode.Internal,
err.stack ? err.stack : err
);
errorHandler(internalError, req, res, next);
}
next();
};
export default errorHandler;

Loading…
Cancel
Save