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