From 0364371c812fddb4774f91e8cfce01aa16a27b33 Mon Sep 17 00:00:00 2001 From: Abimael Martell Date: Wed, 24 Jul 2024 13:43:55 -0700 Subject: [PATCH] Add Error Logging to Exception Handler --- .../nextjs-exception-middleware/with-exception-handling.ts | 5 +++++ packages/nextlove/src/types/index.ts | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/nextlove/src/nextjs-exception-middleware/with-exception-handling.ts b/packages/nextlove/src/nextjs-exception-middleware/with-exception-handling.ts index 02059f425..6b9c9ea08 100644 --- a/packages/nextlove/src/nextjs-exception-middleware/with-exception-handling.ts +++ b/packages/nextlove/src/nextjs-exception-middleware/with-exception-handling.ts @@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next" import { HttpException } from "./http-exceptions" export interface WithExceptionHandlingOptions { + errorLogger?: (error: Error, req: NextApiRequest) => void getErrorContext?: ( req: NextApiRequest, error: Error @@ -17,6 +18,10 @@ const withExceptionHandling = } catch (error: unknown) { let errorContext: any = {} + if (options.errorLogger) { + options.errorLogger(error as Error, req) + } + if (error instanceof Error) { errorContext.stack = error.stack } diff --git a/packages/nextlove/src/types/index.ts b/packages/nextlove/src/types/index.ts index 6bc3bde28..79117d11c 100644 --- a/packages/nextlove/src/types/index.ts +++ b/packages/nextlove/src/types/index.ts @@ -74,7 +74,9 @@ export interface SetupParams< authMiddlewareMap: AuthMW globalMiddlewares: GlobalMW globalMiddlewaresAfterAuth?: GlobalMWAfterAuth - exceptionHandlingMiddleware?: ((next: Function) => Function) | null + exceptionHandlingMiddleware?: ( + next: (req: NextApiRequest, res: NextApiResponse) => Promise + ) => (req: NextApiRequest, res: NextApiResponse) => Promise // These improve OpenAPI generation apiName: string