From 2503756c60000e2654ab3ff899f1bbbd530971e8 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Sun, 2 Sep 2018 21:49:53 -0600 Subject: [PATCH] fix: errorHandler handles internal errors and limit data Only show data in development --- common/ApiError.ts | 4 ++-- server/express/errorHandler.ts | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/common/ApiError.ts b/common/ApiError.ts index 972ee11..ff48b6d 100644 --- a/common/ApiError.ts +++ b/common/ApiError.ts @@ -22,11 +22,11 @@ export default class ApiError extends Error { } } - toJSON() { + toJSON(development: boolean = false) { return { message: this.message, code: this.code, - data: this.data + data: development ? this.data : undefined }; } } diff --git a/server/express/errorHandler.ts b/server/express/errorHandler.ts index e161a0f..6f183ea 100644 --- a/server/express/errorHandler.ts +++ b/server/express/errorHandler.ts @@ -1,6 +1,9 @@ import * as express from "express"; import ApiError from "@common/ApiError"; +import { ErrorCode } from "@common/ErrorCode"; + +const isDev = process.env.NODE_ENV === "development"; const errorHandler: express.ErrorRequestHandler = ( err: any, @@ -9,9 +12,11 @@ const errorHandler: express.ErrorRequestHandler = ( next: express.NextFunction ) => { if (err instanceof ApiError) { - res.status(err.statusCode).json(err.toJSON()); + // TODO: different content-type? + res.status(err.statusCode).json(err.toJSON(isDev)); } else { - next(err); + const internalError = new ApiError("An internal server error has occurred", ErrorCode.Internal); + errorHandler(internalError, req, res, next); } };