-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(router): handle unknown error boundary values #8209
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| '@tanstack/react-router': patch | ||
| '@tanstack/router-core': patch | ||
| '@tanstack/solid-router': patch | ||
| '@tanstack/vue-router': patch | ||
| --- | ||
|
|
||
| Handle arbitrary thrown values in router error boundaries and type caught errors as `unknown`. | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,29 +9,30 @@ export class CatchBoundary extends React.Component<{ | |||||||||||||
| getResetKey: () => unknown | ||||||||||||||
| children: React.ReactNode | ||||||||||||||
| errorComponent?: ErrorRouteComponent | ||||||||||||||
| onCatch?: (error: Error, errorInfo: ErrorInfo) => void | ||||||||||||||
| onCatch?: (error: unknown, errorInfo: ErrorInfo) => void | ||||||||||||||
| }> { | ||||||||||||||
| state = { error: null } as { error: Error | null; resetKey?: unknown } | ||||||||||||||
| // Wrapping caught values keeps every possible thrown value truthy. | ||||||||||||||
| state = { error: 0 } as { error: [unknown] | 0; resetKey?: unknown } | ||||||||||||||
|
|
||||||||||||||
| static getDerivedStateFromProps( | ||||||||||||||
| props: { getResetKey: () => unknown }, | ||||||||||||||
| state: { resetKey?: unknown; error: Error | null }, | ||||||||||||||
| state: { resetKey?: unknown; error: [unknown] | 0 }, | ||||||||||||||
| ) { | ||||||||||||||
| const resetKey = props.getResetKey() | ||||||||||||||
|
|
||||||||||||||
| if (state.error && state.resetKey !== resetKey) { | ||||||||||||||
| return { resetKey, error: null } | ||||||||||||||
| return { resetKey, error: 0 } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return { resetKey } | ||||||||||||||
| } | ||||||||||||||
| static getDerivedStateFromError(error: Error) { | ||||||||||||||
| return { error } | ||||||||||||||
| static getDerivedStateFromError(error: unknown) { | ||||||||||||||
| return { error: [error] } | ||||||||||||||
| } | ||||||||||||||
| reset = () => { | ||||||||||||||
| this.setState({ error: null }) | ||||||||||||||
| this.setState({ error: 0 }) | ||||||||||||||
| } | ||||||||||||||
| componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||||||||||||||
| componentDidCatch(error: unknown, errorInfo: ErrorInfo) { | ||||||||||||||
| this.props.onCatch?.(error, errorInfo) | ||||||||||||||
| } | ||||||||||||||
| render() { | ||||||||||||||
|
|
@@ -40,7 +41,7 @@ export class CatchBoundary extends React.Component<{ | |||||||||||||
| const element = React.createElement( | ||||||||||||||
| this.props.errorComponent ?? ErrorComponent, | ||||||||||||||
| { | ||||||||||||||
| error, | ||||||||||||||
| error: error[0], | ||||||||||||||
| reset: this.reset, | ||||||||||||||
| }, | ||||||||||||||
| ) | ||||||||||||||
|
|
@@ -54,7 +55,7 @@ export class CatchBoundary extends React.Component<{ | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export function ErrorComponent({ error }: { error: any }) { | ||||||||||||||
| export function ErrorComponent({ error }: { error: unknown }) { | ||||||||||||||
| const [show, setShow] = React.useState(process.env.NODE_ENV !== 'production') | ||||||||||||||
|
|
||||||||||||||
| return ( | ||||||||||||||
|
|
@@ -88,7 +89,9 @@ export function ErrorComponent({ error }: { error: any }) { | |||||||||||||
| overflow: 'auto', | ||||||||||||||
| }} | ||||||||||||||
| > | ||||||||||||||
| {error.message ? <code>{error.message}</code> : null} | ||||||||||||||
| {(error as { message?: string } | null)?.message ? ( | ||||||||||||||
| <code>{(error as { message: string }).message}</code> | ||||||||||||||
| ) : null} | ||||||||||||||
|
Comment on lines
+92
to
+94
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. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Render a fallback for non- When a route throws a string, number, Proposed fix- {(error as { message?: string } | null)?.message ? (
- <code>{(error as { message: string }).message}</code>
- ) : null}
+ <code>
+ {error instanceof Error ? error.message : String(error)}
+ </code>📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
| </pre> | ||||||||||||||
| </div> | ||||||||||||||
| ) : null} | ||||||||||||||
|
|
||||||||||||||
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.
When existing TypeScript consumers install this patch, common declarations such as
errorComponent: ({ error }: ErrorComponentProps) => error.messageandonCatch: (error: Error) => ...stop compiling because both public defaults now exposeunknown. Since all four affected packages are stable 1.x releases, publishing these changes as patches allows an ordinary patch upgrade to break downstream builds; either preserve backward-compatible signatures or mark the affected packages for a major release.Useful? React with 👍 / 👎.