From 48d1a5ada5e5fe0975a17b521d3d7a6e1f4cab3b Mon Sep 17 00:00:00 2001 From: Hagop Jamkojian Date: Sun, 16 Feb 2020 22:17:39 +0100 Subject: [PATCH] Fix error stack --- src/middlewares/error.js | 2 +- src/utils/AppError.js | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/middlewares/error.js b/src/middlewares/error.js index e71769e8..697cfef2 100644 --- a/src/middlewares/error.js +++ b/src/middlewares/error.js @@ -8,7 +8,7 @@ const errorConverter = (err, req, res, next) => { if (!(error instanceof AppError)) { const statusCode = error.statusCode || httpStatus.INTERNAL_SERVER_ERROR; const message = error.message || httpStatus[statusCode]; - error = new AppError(statusCode, message, false); + error = new AppError(statusCode, message, false, err.stack); } next(error); }; diff --git a/src/utils/AppError.js b/src/utils/AppError.js index 16396b44..f702d4e7 100644 --- a/src/utils/AppError.js +++ b/src/utils/AppError.js @@ -1,9 +1,13 @@ class AppError extends Error { - constructor(statusCode, message, isOperational = true) { + constructor(statusCode, message, isOperational = true, stack = '') { super(message); this.statusCode = statusCode; this.isOperational = isOperational; - Error.captureStackTrace(this, this.constructor); + if (stack) { + this.stack = stack; + } else { + Error.captureStackTrace(this, this.constructor); + } } }