Next.js Discord

Discord Forum

throw Error in middleware

Unanswered
Standard Chinchilla posted this in #help-forum
Open in Discord
Standard ChinchillaOP
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?

3 Replies

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