using monkey error

This commit is contained in:
Miodec 2021-07-08 12:30:06 +01:00
parent a7aa36c3fa
commit 5fd1121d7b
2 changed files with 7 additions and 13 deletions

View file

@ -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...`);
}
}

View file

@ -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 () => {