🐛 fix error handler

This commit is contained in:
Flam3rboy 2021-02-10 14:34:54 +01:00
parent fe9e5a9e47
commit 9b95834b4b
4 changed files with 26 additions and 18 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -29,7 +29,7 @@
"i18next-http-middleware": "^3.1.0",
"i18next-node-fs-backend": "^2.1.3",
"jsonwebtoken": "^8.5.1",
"lambert-db": "^1.1.6",
"lambert-db": "^1.1.8",
"lambert-server": "^1.0.10",
"missing-native-js-functions": "^1.2.2",
"node-fetch": "^2.6.1"

View File

@ -3,24 +3,29 @@ import { HTTPError } from "lambert-server";
import { FieldError } from "../util/instanceOf";
export function ErrorHandler(error: Error, req: Request, res: Response, next: NextFunction) {
let code = 400;
let httpcode = code;
let message = error?.toString();
let errors = undefined;
try {
let code = 400;
let httpcode = code;
let message = error?.toString();
let errors = undefined;
if (error instanceof HTTPError && error.code) code = httpcode = error.code;
else if (error instanceof FieldError) {
code = Number(error.code);
message = error.message;
errors = error.errors;
} else {
console.error(error);
if (req.server.options.production) {
message = "Internal Server Error";
if (error instanceof HTTPError && error.code) code = httpcode = error.code;
else if (error instanceof FieldError) {
code = Number(error.code);
message = error.message;
errors = error.errors;
} else {
console.error(error);
if (req.server.options.production) {
message = "Internal Server Error";
}
code = httpcode = 500;
}
code = httpcode = 500;
}
res.status(httpcode).json({ code: code, message, errors });
return next();
res.status(httpcode).json({ code: code, message, errors });
return next();
} catch (error) {
console.error(error);
return res.status(500).json({ code: 500, message: "Internal Server Error" });
}
}

View File

@ -2,6 +2,9 @@ import { NextFunction, Request, Response } from "express";
import Config from "../util/Config";
import { db } from "discord-server-util";
// TODO: use mongodb ttl index
// TODO: increment count on serverside
export async function GlobalRateLimit(req: Request, res: Response, next: NextFunction) {
if (!Config.get().limits.rate.ip.enabled) return next();