🐛 fix error handler
This commit is contained in:
parent
fe9e5a9e47
commit
9b95834b4b
14
package-lock.json
generated
14
package-lock.json
generated
@ -18,7 +18,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"
|
||||||
@ -1078,9 +1078,9 @@
|
|||||||
"integrity": "sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ=="
|
"integrity": "sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ=="
|
||||||
},
|
},
|
||||||
"node_modules/lambert-db": {
|
"node_modules/lambert-db": {
|
||||||
"version": "1.1.6",
|
"version": "1.1.8",
|
||||||
"resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.6.tgz",
|
"resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.8.tgz",
|
||||||
"integrity": "sha512-FbUNAbgPYUvfJSxLdcfIk6w8J+Z4IYZJasVURBMm4eOeLm//kgGeOi+SrJqqa5z+gaCJ65ENIVs2hd+Cj2Efbw==",
|
"integrity": "sha512-EyIEYLppS54tXsxIXGuS0l/0c+tF86KvaXRR92yRcGp5CHWRxPfUqV0KX/Ft7OkqtRTK5tDuJ/Bb4kUCXh2VUg==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"missing-native-js-functions": "^1.1.8",
|
"missing-native-js-functions": "^1.1.8",
|
||||||
"mongodb": "^3.6.2",
|
"mongodb": "^3.6.2",
|
||||||
@ -3247,9 +3247,9 @@
|
|||||||
"integrity": "sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ=="
|
"integrity": "sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ=="
|
||||||
},
|
},
|
||||||
"lambert-db": {
|
"lambert-db": {
|
||||||
"version": "1.1.6",
|
"version": "1.1.8",
|
||||||
"resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.6.tgz",
|
"resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.8.tgz",
|
||||||
"integrity": "sha512-FbUNAbgPYUvfJSxLdcfIk6w8J+Z4IYZJasVURBMm4eOeLm//kgGeOi+SrJqqa5z+gaCJ65ENIVs2hd+Cj2Efbw==",
|
"integrity": "sha512-EyIEYLppS54tXsxIXGuS0l/0c+tF86KvaXRR92yRcGp5CHWRxPfUqV0KX/Ft7OkqtRTK5tDuJ/Bb4kUCXh2VUg==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"missing-native-js-functions": "^1.1.8",
|
"missing-native-js-functions": "^1.1.8",
|
||||||
"mongodb": "^3.6.2",
|
"mongodb": "^3.6.2",
|
||||||
|
@ -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();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user