From be75c9a3e5ce13d3544e828d92af282f60680f38 Mon Sep 17 00:00:00 2001 From: Miodec Date: Thu, 8 Jul 2021 15:58:11 +0100 Subject: [PATCH] updated error handling --- backend/handlers/error.js | 19 +++++++++++++++---- backend/server.js | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/backend/handlers/error.js b/backend/handlers/error.js index 17db4c9dd..db27ca359 100644 --- a/backend/handlers/error.js +++ b/backend/handlers/error.js @@ -1,11 +1,22 @@ const uuid = require("uuid"); class MonkeyError { - constructor(status, message = "Internal Server Error", stack = null) { + constructor(status, message, stack = null) { this.status = status ?? 500; - this.errorID = uuid.v4(); - this.message = - process.env.MODE === "dev" ? (stack ? String(stack) : message) : message; + let errorID = uuid.v4(); + if (this.status === 500) { + this.message = + process.env.MODE === "dev" + ? String(stack) + : "Internal Server Error " + errorID; + } else { + this.message = + process.env.MODE === "dev" + ? stack + ? String(stack) + : message + : message; + } console.log(`ErrorID: ${this.errorID} logged...`); } } diff --git a/backend/server.js b/backend/server.js index 8c5fcfdbd..69859c175 100644 --- a/backend/server.js +++ b/backend/server.js @@ -27,7 +27,7 @@ app.use("/result", resultRouter); app.use(function (e, req, res, next) { console.log("Error", e); - let monkeyError = new MonkeyError(e.status, undefined, e.stack); + let monkeyError = new MonkeyError(e.status, e.message, e.stack); return res.status(e.status || 500).json(monkeyError); });