only logging to db when error is 500 and env is not dev

This commit is contained in:
Miodec 2021-07-09 21:18:50 +01:00
parent 9676e47ea0
commit edd453bfef
2 changed files with 16 additions and 4 deletions

View file

@ -1,7 +1,8 @@
const uuid = require("uuid");
const { mongoDB } = require("../init/mongodb");
class MonkeyError {
constructor(status, message, stack = null) {
constructor(status, message, stack = null, uid) {
this.status = status ?? 500;
this.errorID = uuid.v4();
if (this.status === 500) {
@ -19,7 +20,18 @@ class MonkeyError {
: message
: message;
}
console.log(`ErrorID: ${this.errorID} logged...`);
console.log("Error", message, stack);
if (process.env.MODE !== "dev" && this.status === 500) {
mongoDB()
.collection("errors")
.insertOne({
_id: this.errorID,
timestamp: Date.now(),
uid,
message,
stack,
});
}
}
}

View file

@ -28,8 +28,8 @@ const presetRouter = require("./api/routes/preset");
app.use("/presets", presetRouter);
app.use(function (e, req, res, next) {
console.log("Error", e);
let monkeyError = new MonkeyError(e.status, e.message, e.stack);
const { uid } = req.decodedToken;
let monkeyError = new MonkeyError(e.status, e.message, e.stack, uid);
return res.status(e.status || 500).json(monkeyError);
});