How to return an error with a code and message from an app server route?
Answered
Giant panda posted this in #help-forum
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:
How do I change the status code to 500? How can I add message and type fields to the response?
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 })
}2 Replies
@Giant panda 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?
export async function GET(request: Request) {
return Response.json({ type:"error", message:"error message" }, { status: 500 })
}Answer
GET endpoint is static by default. you can make it dynamic by
export const dynamic = 'force-dynamic'