🐛 fix error handler
This commit is contained in:
parent
fe9e5a9e47
commit
9b95834b4b
BIN
package-lock.json
generated
BIN
package-lock.json
generated
Binary file not shown.
@ -29,7 +29,7 @@
|
|||||||
"i18next-http-middleware": "^3.1.0",
|
"i18next-http-middleware": "^3.1.0",
|
||||||
"i18next-node-fs-backend": "^2.1.3",
|
"i18next-node-fs-backend": "^2.1.3",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"lambert-db": "^1.1.6",
|
"lambert-db": "^1.1.8",
|
||||||
"lambert-server": "^1.0.10",
|
"lambert-server": "^1.0.10",
|
||||||
"missing-native-js-functions": "^1.2.2",
|
"missing-native-js-functions": "^1.2.2",
|
||||||
"node-fetch": "^2.6.1"
|
"node-fetch": "^2.6.1"
|
||||||
|
|||||||
@ -3,24 +3,29 @@ import { HTTPError } from "lambert-server";
|
|||||||
import { FieldError } from "../util/instanceOf";
|
import { FieldError } from "../util/instanceOf";
|
||||||
|
|
||||||
export function ErrorHandler(error: Error, req: Request, res: Response, next: NextFunction) {
|
export function ErrorHandler(error: Error, req: Request, res: Response, next: NextFunction) {
|
||||||
let code = 400;
|
try {
|
||||||
let httpcode = code;
|
let code = 400;
|
||||||
let message = error?.toString();
|
let httpcode = code;
|
||||||
let errors = undefined;
|
let message = error?.toString();
|
||||||
|
let errors = undefined;
|
||||||
|
|
||||||
if (error instanceof HTTPError && error.code) code = httpcode = error.code;
|
if (error instanceof HTTPError && error.code) code = httpcode = error.code;
|
||||||
else if (error instanceof FieldError) {
|
else if (error instanceof FieldError) {
|
||||||
code = Number(error.code);
|
code = Number(error.code);
|
||||||
message = error.message;
|
message = error.message;
|
||||||
errors = error.errors;
|
errors = error.errors;
|
||||||
} else {
|
} else {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
if (req.server.options.production) {
|
if (req.server.options.production) {
|
||||||
message = "Internal Server Error";
|
message = "Internal Server Error";
|
||||||
|
}
|
||||||
|
code = httpcode = 500;
|
||||||
}
|
}
|
||||||
code = httpcode = 500;
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(httpcode).json({ code: code, message, errors });
|
res.status(httpcode).json({ code: code, message, errors });
|
||||||
return next();
|
return next();
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return res.status(500).json({ code: 500, message: "Internal Server Error" });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,9 @@ import { NextFunction, Request, Response } from "express";
|
|||||||
import Config from "../util/Config";
|
import Config from "../util/Config";
|
||||||
import { db } from "discord-server-util";
|
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) {
|
export async function GlobalRateLimit(req: Request, res: Response, next: NextFunction) {
|
||||||
if (!Config.get().limits.rate.ip.enabled) return next();
|
if (!Config.get().limits.rate.ip.enabled) return next();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user