Next.js Discord

Discord Forum

How to return an error with a code and message from an app server route?

Answered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
I can't figure out how to return an error from a route.tsx. I havent been able to find this in the docs.

My code looks like this:
export async function GET(request: Request) {

  return Response.error()
}


How do I change the status code to 500? How can I add message and type fields to the response?
Answered by Ray
export async function GET(request: Request) {
  return Response.json({ type:"error", message:"error message" }, { status: 500 })
}
View full answer

2 Replies

Answer
GET endpoint is static by default. you can make it dynamic by export const dynamic = 'force-dynamic'