-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(react-router): handle falsy thrown values in CatchBoundary #8199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lx3133584
wants to merge
1
commit into
TanStack:main
Choose a base branch
from
lx3133584:fix/react-router-catchboundary-falsy-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+46
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -868,3 +868,36 @@ describe('notFoundComponent is rendered when an error is thrown in params.parse' | |
| expect(notFoundComponent).toBeInTheDocument() | ||
| }) | ||
| }) | ||
|
|
||
| test.each([ | ||
| { desc: 'undefined', value: undefined, expected: 'undefined' }, | ||
| { desc: 'null', value: null, expected: 'null' }, | ||
| { desc: 'empty string', value: '', expected: '""' }, | ||
|
Comment on lines
+873
to
+875
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Add the The PR supports 🤖 Prompt for AI Agents |
||
| ])( | ||
| 'errorComponent is rendered when component throws falsy value: $desc', | ||
| async ({ value, expected }) => { | ||
| const rootRoute = createRootRoute() | ||
| const indexRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/', | ||
| component: function Home() { | ||
| throw value | ||
| }, | ||
| errorComponent: ({ error }) => ( | ||
| <div>Caught falsy error: {JSON.stringify(error) ?? 'undefined'}</div> | ||
| ), | ||
| }) | ||
|
|
||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([indexRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/'] }), | ||
| }) | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
|
|
||
| expect( | ||
| await screen.findByText(`Caught falsy error: ${expected}`), | ||
| ).toBeInTheDocument() | ||
| }, | ||
| ) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: TanStack/router
Length of output: 21781
🏁 Script executed:
Repository: TanStack/router
Length of output: 8002
🏁 Script executed:
Repository: TanStack/router
Length of output: 50371
🏁 Script executed:
Repository: TanStack/router
Length of output: 14806
🌐 Web query:
React error boundaries getDerivedStateFromError componentDidCatch non-Error values null undefined thrown value React 19 contract💡 Result:
In React, including React 19, Error Boundaries catch values thrown during rendering, lifecycle methods, and constructors of the tree below them [1][2][3]. When a value is thrown, React captures that value and passes it as the
errorargument tostatic getDerivedStateFromError(error)andcomponentDidCatch(error, info)[2][4]. The React error boundary contract does not strictly enforce that the thrown value must be an instance of the JavaScriptErrorclass [3]. While standard practice and documentation often refer to this argument as an "error" and typically expect anErrorobject—especially for access tostacktraces—React will pass whatever value was thrown to these lifecycle methods, includingnull,undefined, strings, numbers, or objects [3][4]. Key considerations for non-Error values include: 1. Handling Logic: If you rely onerror.messageorerror.stack, you must implement defensive checks (e.g.,error instanceof Error) within your lifecycle methods to avoid runtime exceptions when a non-Error value is caught [5][4]. 2. React 19 Behavior: React 19 has improved error handling by reducing duplicate logs and consolidating the error recovery process [6]. However, the underlying contract regarding the types of values captured by Error Boundaries remains consistent with previous versions; the boundary acts as a generic catch-all for values thrown during the render phase [2][6]. 3. Best Practices: It is highly recommended to only throwErrorinstances [3]. If you need to handle specific application-level errors, perform aninstanceofcheck or check for specific properties withingetDerivedStateFromErrorto determine if the error should be handled by that boundary or allowed to propagate to a higher one [5]. In summary, while the React contract technically permits catching non-Error values (likenullorundefined), your application code should be prepared to handle these values safely to prevent the Error Boundary itself from throwing an error, which would cause the error to propagate further up the tree [1][5][4].Citations:
Type caught values as
unknownthroughout the error boundary.React forwards any thrown value to
getDerivedStateFromErrorandcomponentDidCatch. The boundary stores that value, passes it toerrorComponent, and forwards it toonCatch. Update these types, plusErrorComponentPropsand publiconCatchcontracts, tounknownor a shared supported type. Narrow values before property access.🤖 Prompt for AI Agents