From 5fd1121d7b3b85599c3f13cfe7fe562b2f778167 Mon Sep 17 00:00:00 2001 From: Miodec Date: Thu, 8 Jul 2021 12:30:06 +0100 Subject: [PATCH] using monkey error --- backend/handlers/error.js | 16 ++++------------ backend/server.js | 4 +++- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/backend/handlers/error.js b/backend/handlers/error.js index 95821c41e..17db4c9dd 100644 --- a/backend/handlers/error.js +++ b/backend/handlers/error.js @@ -1,19 +1,11 @@ const uuid = require("uuid"); class MonkeyError { - status = 500; - message = "Internal Server Error"; - errorID = ""; - - constructor(status, message, stack = null) { - this.status = status; - this.message = - process.env.MODE === "DEVELOPMENT" - ? stack - ? String(stack) - : message - : message; + constructor(status, message = "Internal Server Error", stack = null) { + this.status = status ?? 500; this.errorID = uuid.v4(); + 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 0ea055ef5..8c5fcfdbd 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1,6 +1,7 @@ const express = require("express"); const { config } = require("dotenv"); const path = require("path"); +const MonkeyError = require("./handlers/error"); config({ path: path.join(__dirname, ".env") }); const cors = require("cors"); @@ -26,7 +27,8 @@ app.use("/result", resultRouter); app.use(function (e, req, res, next) { console.log("Error", e); - return res.status(e.status || 500).json(e || {}); + let monkeyError = new MonkeyError(e.status, undefined, e.stack); + return res.status(e.status || 500).json(monkeyError); }); app.listen(PORT, async () => {