add custon express error handler

This commit is contained in:
hippoz 2022-08-05 04:32:10 +03:00
parent 79efc49cd2
commit 6fe398c82a
Signed by: hippoz
GPG key ID: 7C52899193467641
2 changed files with 11 additions and 2 deletions

View file

@ -6,7 +6,8 @@ export const errors = {
FORBIDDEN_DUE_TO_MISSING_PERMISSIONS: { code: 6005, message: "Forbidden due to missing permission(s)" },
BAD_SUPERUSER_KEY: { code: 6006, message: "Bad superuser key" },
GOT_NO_DATABASE_DATA: { code: 7001, message: "Unexpectedly got no data from database" },
FEATURE_DISABLED: { code: 7002, message: "This feature is disabled" }
FEATURE_DISABLED: { code: 7002, message: "This feature is disabled" },
INTERNAL_ERROR: { code: 7003, message: "Internal server error" }
};
export const gatewayErrors = {

View file

@ -1,7 +1,8 @@
import express, { Application, json } from "express";
import express, { Application, ErrorRequestHandler, json } from "express";
import usersRouter from "./routes/api/v1/users";
import channelsRouter from "./routes/api/v1/channels";
import messagesRouter from "./routes/api/v1/messages";
import { errors } from "./errors";
export default function(app: Application) {
app.use(json());
@ -9,4 +10,11 @@ export default function(app: Application) {
app.use("/api/v1/channels", channelsRouter);
app.use("/api/v1/messages", messagesRouter);
app.use("/", express.static("frontend/public"));
const errorHandler: ErrorRequestHandler = (error, req, res, next) => {
console.error("error: while handling request", error);
res.status(500).json({ ...errors.INTERNAL_ERROR });
};
app.use(errorHandler);
};