Skip to content

Commit

Permalink
Merge pull request #8 from Prakharnagore/errorhandling
Browse files Browse the repository at this point in the history
add error handling middleware
  • Loading branch information
Prakharnagore committed Aug 24, 2024
2 parents 6132bcc + c3d09e5 commit 0988d4a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 23 deletions.
26 changes: 23 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "^16.11.10",
"@types/supertest": "^6.0.2",
"@types/uuid": "^10.0.0",
"@types/winston": "^2.4.4",
"@typescript-eslint/eslint-plugin": "^7.3.1",
"@typescript-eslint/parser": "^7.3.1",
Expand Down Expand Up @@ -70,6 +71,7 @@
"reflect-metadata": "^0.1.13",
"rsa-pem-to-jwk": "^1.1.3",
"typeorm": "0.3.20",
"uuid": "^10.0.0",
"winston": "^3.12.0"
}
}
22 changes: 3 additions & 19 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import "reflect-metadata";
import cors from "cors";
import express, { Request, Response, NextFunction } from "express";
import express from "express";
import cookieParser from "cookie-parser";
import logger from "./config/logger";
import { HttpError } from "http-errors";
import authRouter from "./routes/auth";
import tenantRouter from "./routes/tenant";
import userRouter from "./routes/user";
import { Config } from "./config";
import { globalErrorHandler } from "./middlewares/globalErrorHandler";

const app = express();
app.use(
Expand All @@ -27,21 +26,6 @@ app.get("/", (req, res) => {
app.use("/auth", authRouter);
app.use("/tenants", tenantRouter);
app.use("/users", userRouter);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
app.use((err: HttpError, req: Request, res: Response, next: NextFunction) => {
logger.error(err.message);
const statusCode = err.statusCode || err.status || 500;
res.status(statusCode).json({
errors: [
{
type: err.name,
msg: err.message,
path: "",
location: "",
},
],
});
});
app.use(globalErrorHandler);

export default app;
2 changes: 1 addition & 1 deletion src/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class UserController {
// Validation
const result = validationResult(req);
if (!result.isEmpty()) {
return res.status(400).json({ errors: result.array() });
return next(createHttpError(400, result.array()[0].msg as string));
}

const { firstName, lastName, email, password, tenantId, role } =
Expand Down
40 changes: 40 additions & 0 deletions src/middlewares/globalErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Request, Response, NextFunction } from "express";
import { HttpError } from "http-errors";
import logger from "../config/logger";
import { v4 as uuidv4 } from "uuid";

export const globalErrorHandler = (
err: HttpError,
req: Request,
res: Response,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
next: NextFunction,
) => {
const errorId = uuidv4();
const statusCode = err.statusCode || err.status || 500;

const isProduction = process.env.NODE_ENV === "production";
const message = isProduction ? "Internal server error" : err.message;

logger.error(err.message, {
id: errorId,
statusCode,
error: err.stack,
path: req.path,
method: req.method,
});

res.status(statusCode).json({
errors: [
{
ref: errorId,
type: err.name,
msg: message,
path: req.path,
method: req.method,
location: "server",
stack: isProduction ? null : err.stack,
},
],
});
};

0 comments on commit 0988d4a

Please sign in to comment.