From 5f367110db4e37eac07b074a0874258f6a285a1e Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Tue, 10 Sep 2024 12:40:56 -0300 Subject: [PATCH] feat: handle server errors in api --- apps/api/src/app.ts | 2 ++ apps/api/src/common/middleware/cache.middleware.ts | 7 ++++--- .../common/middleware/generalError.middleware.ts | 14 ++++++++++++++ apps/api/src/common/middleware/index.ts | 1 + 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 apps/api/src/common/middleware/generalError.middleware.ts diff --git a/apps/api/src/app.ts b/apps/api/src/app.ts index 458f6dd..9da4dc6 100644 --- a/apps/api/src/app.ts +++ b/apps/api/src/app.ts @@ -5,6 +5,7 @@ import { ILogger } from "@zkchainhub/shared"; import { listRoutes, setupOpenApiConfiguration as setupOpenApi } from "./api-docs/index.js"; import { ConfigType } from "./common/config/index.js"; +import { generalErrorHandler } from "./common/middleware/generalError.middleware.js"; import { requestLogger } from "./common/middleware/requestLogger.middleware.js"; import { zodErrorHandler } from "./common/middleware/zodError.middleware.js"; import { BaseRouter } from "./common/routes/baseRouter.js"; @@ -64,5 +65,6 @@ export class App { private initializeErrorHandling(): void { this.app.use(zodErrorHandler); + this.app.use(generalErrorHandler); } } diff --git a/apps/api/src/common/middleware/cache.middleware.ts b/apps/api/src/common/middleware/cache.middleware.ts index c6fec22..c7e87e9 100644 --- a/apps/api/src/common/middleware/cache.middleware.ts +++ b/apps/api/src/common/middleware/cache.middleware.ts @@ -22,10 +22,11 @@ export function cacheMiddleware(args: { ttl: number } = { ttl: DEFAULT_TTL }) { // Store the original send and json functions const originalJson = res.json.bind(res); // Override the json function - res.json = (body): Response => { - // Cache the response body - cache.set(key, body, args.ttl); + if (!("errors" in body)) { + // Cache the response body if it is not an error response + cache.set(key, body, args.ttl); + } // Call the original json function with the response body return originalJson(body); }; diff --git a/apps/api/src/common/middleware/generalError.middleware.ts b/apps/api/src/common/middleware/generalError.middleware.ts new file mode 100644 index 0000000..795e447 --- /dev/null +++ b/apps/api/src/common/middleware/generalError.middleware.ts @@ -0,0 +1,14 @@ +import { isNativeError } from "util/types"; +import { NextFunction, Request, Response } from "express"; + +export const generalErrorHandler = ( + err: unknown, + req: Request, + res: Response, + _next: NextFunction, +) => { + return res.status(500).json({ + message: "Internal server error", + errors: isNativeError(err) ? err.message : err, + }); +}; diff --git a/apps/api/src/common/middleware/index.ts b/apps/api/src/common/middleware/index.ts index ed86ec0..c82314b 100644 --- a/apps/api/src/common/middleware/index.ts +++ b/apps/api/src/common/middleware/index.ts @@ -1,2 +1,3 @@ export * from "./zodError.middleware.js"; export * from "./requestLogger.middleware.js"; +export * from "./generalError.middleware.js";