Suggestion: unauthorized()
and forbidden()
error handling APIs (like notFound()
)
#4268
fellipeutaka
started this conversation in
Ideas
Replies: 1 comment
-
good idea |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! First of all, thank you for the amazing work on TanStack Router. The flexibility and DX are really impressive! 🙌
I'm here to ask for your thoughts on a potential feature to improve how we handle common HTTP errors like
401 Unauthorized
and403 Forbidden
— similarly to hownotFound()
is currently supported.📌 Problem
Right now, the router offers great ergonomics for redirects and not-found cases using
throw redirect()
andthrow notFound()
, and lets us define anotFoundComponent
to display friendly UI when a 404 occurs.However, handling other frequent errors like 403 Forbidden and 401 Unauthorized typically requires custom logic in an
errorComponent
, for example:This works, but requires extra boilerplate and logic inside a error boundary.
✅ Proposal
Introduce new functions similar to
notFound()
:throw unauthorized()
throw forbidden()
unauthorizedComponent
forbiddenComponent
Example:
This would allow for a more declarative and composable approach to common authorization errors, without pushing all logic into a global error handler.
🔍 Inspiration
This idea is inspired by the experimental
unauthorized
andforbidden
file conventions in Next.js 15.1+, where callingunauthorized()
orforbidden()
during rendering triggers a specific UI file and returns appropriate status codes (401
or403
):unauthorized()
+app/unauthorized.tsx
forbidden()
+app/forbidden.tsx
It’s a neat pattern that keeps intent explicit and decouples the rendering of error states from the rest of the app.
💬 Question
Would something like this make sense within TanStack Router’s design principles?
I’d love to hear your thoughts and see if this aligns with the router’s goals. If it does, I’d be happy to help brainstorm the API further or even contribute.
Thanks again for your work — and for considering the suggestion!
Beta Was this translation helpful? Give feedback.
All reactions