throw Error in middleware
Unanswered
Standard Chinchilla posted this in #help-forum
Standard ChinchillaOP
Hey guys, tried to google without much luck. Is there anyway to throw an error in
in my middleware, I get the nextjs unhandled error page?
middleware.ts which will then use the global-error.tsx or error.tsx templates? Any the moment if i do:throw new Error("Unauthorized");in my middleware, I get the nextjs unhandled error page?
3 Replies
@Standard Chinchilla Hey guys, tried to google without much luck. Is there anyway to throw an error in `middleware.ts` which will then use the `global-error.tsx` or `error.tsx` templates? Any the moment if i do:
`throw new Error("Unauthorized");`
in my middleware, I get the nextjs unhandled error page?
middleware is not part of the app router. throwing an error there will simply crash the app, the error doesn't get sent to the error boundaries in the app router or the pages router
Standard ChinchillaOP
Is there a way around this? Basically i just want to display a nicer "Unauthorized" page.
@Standard Chinchilla Is there a way around this? Basically i just want to display a nicer "Unauthorized" page.
Make a, say, /401 page and rewrite the request to /401