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 {
toJSON(development: boolean = false) { toJSON(development: boolean = false) {
return { return {
message: this.message, message: this.message,
statusCode: this.statusCode,
code: this.code, code: this.code,
data: development ? this.data : undefined data: development ? this.data : undefined
}; };

6
server/express/errorHandler.ts

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

Loading…
Cancel
Save