Skip to content

Commit

Permalink
Add Error Logging to Exception Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
abimaelmartell committed Jul 24, 2024
1 parent aa691bd commit 0364371
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion packages/nextlove/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>
) => (req: NextApiRequest, res: NextApiResponse) => Promise<any>

// These improve OpenAPI generation
apiName: string
Expand Down

0 comments on commit 0364371

Please sign in to comment.