updated error handling

This commit is contained in:
Miodec 2021-07-08 15:58:11 +01:00
parent 78847dd026
commit be75c9a3e5
2 changed files with 16 additions and 5 deletions

View file

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

View file

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