mirror of
https://github.com/spacebarchat/server.git
synced 2026-08-29 03:18:40 +00:00
🔊 verbose error handler
This commit is contained in:
+3
-2
@@ -5,7 +5,7 @@ import { Authentication, CORS } from "./middlewares/";
|
||||
import { Config, db, initEvent } from "@fosscord/util";
|
||||
import { ErrorHandler } from "./middlewares/ErrorHandler";
|
||||
import { BodyParser } from "./middlewares/BodyParser";
|
||||
import { Router, Request, Response } from "express";
|
||||
import { Router, Request, Response, NextFunction } from "express";
|
||||
import mongoose from "mongoose";
|
||||
import path from "path";
|
||||
import { initRateLimits } from "./middlewares/RateLimit";
|
||||
@@ -69,7 +69,8 @@ export class FosscordServer extends Server {
|
||||
|
||||
this.routes = await this.registerRoutes(path.join(__dirname, "routes", "/"));
|
||||
|
||||
api.use("*", (req: Request, res: Response, next) => {
|
||||
api.use("*", (error: any, req: Request, res: Response, next: NextFunction) => {
|
||||
if (error) return next(error);
|
||||
res.status(404).json({
|
||||
message: "404: Not Found",
|
||||
code: 0
|
||||
|
||||
@@ -3,6 +3,8 @@ import { HTTPError } from "lambert-server";
|
||||
import { FieldError } from "../util/instanceOf";
|
||||
|
||||
export function ErrorHandler(error: Error, req: Request, res: Response, next: NextFunction) {
|
||||
if (!error) next();
|
||||
|
||||
try {
|
||||
let code = 400;
|
||||
let httpcode = code;
|
||||
@@ -24,9 +26,11 @@ export function ErrorHandler(error: Error, req: Request, res: Response, next: Ne
|
||||
|
||||
if (httpcode > 511) httpcode = 400;
|
||||
|
||||
console.error(`[Error] ${code} ${req.url} ${message}`, errors || error);
|
||||
|
||||
res.status(httpcode).json({ code: code, message, errors });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
console.error(`[Internal Server Error] 500`, error);
|
||||
return res.status(500).json({ code: 500, message: "Internal Server Error" });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user