inserting monkey error into the database later to avoid recursive errors

This commit is contained in:
Miodec 2021-08-17 20:00:29 +01:00
parent 133ae13503
commit 5cfa792eb2
2 changed files with 10 additions and 13 deletions

View file

@ -1,5 +1,4 @@
const uuid = require("uuid");
const { mongoDB } = require("../init/mongodb");
class MonkeyError {
constructor(status, message, stack = null, uid) {
@ -14,18 +13,6 @@ class MonkeyError {
? String(message)
: message
: "Internal Server Error " + this.errorID;
console.log("Error", message, stack);
if (process.env.MODE !== "dev" && this.status > 400) {
mongoDB().collection("errors").insertOne({
_id: this.errorID,
timestamp: Date.now(),
status: this.status,
uid,
message,
stack,
});
}
}
}

View file

@ -33,6 +33,16 @@ app.use(function (e, req, res, next) {
uid = req.decodedToken.uid;
}
let monkeyError = new MonkeyError(e.status, e.message, e.stack, uid);
if (process.env.MODE !== "dev" && monkeyError.status > 400) {
connectDB().collection("errors").insertOne({
_id: monkeyError.errorID,
timestamp: Date.now(),
status: monkeyError.status,
uid: monkeyError.uid,
message: monkeyError.message,
stack: monkeyError.stack,
});
}
return res.status(e.status || 500).json(monkeyError);
});