Next.js Discord

Discord Forum

How to return an error from the app router?

Unanswered
Black-headed Gull posted this in #help-forum
Open in Discord
Black-headed GullOP
I have a route in my API directory where I'm trying to return an error:

if ( !response.ok ) { let err = new Error(HTTP status code: ${response.status}); err.response =${response.url} - ${response.statusText}; err.status = response.status; throw err; }

that is then caught in my try/catch's catch where I then return NextResponse.status( e.status ).json( e.response );

What is the correct way to send back the error? I have a service that calls the API and I'm getting a 500 error returned there.

0 Replies