mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-02-04 04:51:16 +08:00
only logging to db when error is 500 and env is not dev
This commit is contained in:
parent
9676e47ea0
commit
edd453bfef
2 changed files with 16 additions and 4 deletions
|
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue