How to return a custom NextResponse when error handling via instrumentation onRequestError? #84412
Replies: 1 comment 9 replies
-
The problem is one of execution flow in Next.js: If your route handler uses a try...catch block to return an error, Next.js sees this as a handled execution. The onRequestError callback only fires on a truly uncaught exception that bubbles up to the network layer. trigger onRequestError while controlling the response: Stop using return NextResponse.json({ error: '...' }, { status: 400 }) inside your catch block. This prevents onRequestError from ever running. Instead, create and throw a custom error inside your route handler's catch block. This ensures an uncaught exception fires the onRequestError function. In your onRequestError function, check the type of the thrown error and return the custom NextResponse there. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Looking for guidance on how to return a custom NextResponse when handling errors with the onRequestError callback.
If I catch the error in the route handler to return a custom NextResponse then the onRequestError function is never called.
Beta Was this translation helpful? Give feedback.
All reactions