From 4963e31473a237f878212e7da4267787335ed537 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Mon, 3 Sep 2018 18:06:14 -0600 Subject: [PATCH] fix: Error handling --- common/ApiError.ts | 1 + server/express/errorHandler.ts | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/common/ApiError.ts b/common/ApiError.ts index ff48b6d..fb5a81e 100644 --- a/common/ApiError.ts +++ b/common/ApiError.ts @@ -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 }; diff --git a/server/express/errorHandler.ts b/server/express/errorHandler.ts index cadc244..7a8d55b 100644 --- a/server/express/errorHandler.ts +++ b/server/express/errorHandler.ts @@ -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;