-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
fix(preact-query): propagate falsy errors to the error boundary #11309
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/preact-query': patch | ||
| --- | ||
|
|
||
| fix(preact-query): propagate falsy errors to the error boundary |
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
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
Oops, something went wrong.
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.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🌐 Web query:
For Preact 10.28.0, what happens when a component throwsundefinedornullwhile an ErrorBoundary is active?💡 Result:
In Preact 10.28.0, when a component throws a value—such as
nullorundefined—within the subtree of an active ErrorBoundary, Preact treats it as a caught error [1]. Preact's error handling mechanisms, includingcomponentDidCatchand theuseErrorBoundaryhook, are designed to capture exceptions thrown during rendering, lifecycle methods, and constructors [1][2]. If a child component performs athrow null;orthrow undefined;, the ErrorBoundary will catch this value as the error object [1]. Because theerrorparameter provided byuseErrorBoundaryorcomponentDidCatchcan be any type, the error object received will be exactly the value thrown (i.e.,nullorundefined) [1]. If your error fallback UI attempts to access properties on this object—such aserror.messageorerror.toString()—it may cause the error boundary itself to throw a new error, leading to a crash or an unrendered UI, as these properties do not exist onnullorundefined[1][2]. It is recommended to implement defensive checks in your fallback UI to handle non-object errors gracefully [1][3]: function ErrorFallback({ error, resetError }) { const errorMessage = error instanceof Error? error.message: String(error); return (Something went wrong: {errorMessage}
Try AgainCitations:
🏁 Script executed:
Repository: TanStack/query
Length of output: 50373
🏁 Script executed:
Repository: TanStack/query
Length of output: 31498
🌐 Web query:
Preact 10.28.0 source catch error null undefined error boundary componentDidCatch error handling e.then💡 Result:
In Preact 10.28.0 and other recent versions, error boundaries (implemented via componentDidCatch or getDerivedStateFromError) are designed to catch rendering errors in child components [1][2]. They do not catch errors that occur in event handlers, asynchronous code (e.g., promises), or errors within the error boundary itself [3][4]. Regarding the specific error "e.then is not a function": This often occurs in the context of Preact's Suspense mechanism [5][6]. Preact uses Promises (which have a.then method) to handle "suspending" components—effectively pausing rendering until data is available [6]. If a component throws an error that is actually a Promise (a common pattern for suspense), and it is incorrectly handled—or if a regular error is thrown in a context expecting a Promise (like a rendering loop where the system is looking for a suspender)—the runtime may attempt to call.then on that error [5][7]. If the error is not a valid Promise (e.g., it is null, undefined, or a standard Error object), this results in the TypeError: "e.then is not a function" [5]. Recent Preact-related updates have specifically addressed this by ensuring that errors thrown during rendering are correctly distinguished from suspense-related Promises [5][7]. For example, render-to-string implementations have been updated to include logic similar to the following to prevent this error [7]: try { return renderChildren; } catch (e) { // If the error does not have a 'then' method, it is a real error, not a suspense promise. if (!e || typeof e.then!== 'function') throw e; // Otherwise, handle as a suspense promise... return e.then(...); } If you are encountering this error, it is likely that: 1. You are using Suspense or lazy-loaded components [6]. 2. A component in your tree is throwing an object that is not a proper Promise, or the error handling logic is incorrectly treating a standard error as a Suspense promise [5][7]. 3. Ensure that all standard errors are thrown as Error objects and that any custom suspense-like behavior correctly implements the expected Promise interface [5][7].
Citations:
🌐 Web query:
site:github.com/preactjs/preact/blob/10.28.0/src/diff/index.js "e.then" "componentDidCatch"💡 Result:
In Preact
10.28.0,src/diff/index.js:e.thenappears at lines 277 and 296, where thrown thenables are treated as suspended rendering. (raw.githubusercontent.com)componentDidCatchdoes not appear directly in this file. Error handling is delegated tooptions._catchError(e, newVNode, oldVNode)at line 298. (raw.githubusercontent.com)Citations:
🌐 Web query:
site:github.com/preactjs/preact/blob/10.28.0/src/diff/index.js "catch (e)" error boundary💡 Result:
In Preact 10.28.0, the relevant error-boundary path is:
catch (e)at approximately lines 2049–2096options._catchError(e, newVNode, oldVNode), which performs error-boundary handling. (github.com)Other
catch (e)blocks handle lifecycle callbacks, refs, and unmounting—not the main render error boundary.Citations:
🌐 Web query:
raw.githubusercontent.com preact 10.28.0 src/diff/index.js lines 260 305 catch e.then _catchError💡 Result:
In Preact version 10.28.0, the
difffunction withinsrc/diff/index.jshandles errors that occur during the reconciliation process by invokingoptions._catchError. The logic involvingif (!e.then)is used to determine whether to mark a virtual node (VNode) as needing to be re-rendered (forced) when an error occurs [1]. Specifically, when an error is caught in thediffcycle, Preact checks if the error is a promise (e.then), which typically indicates a Suspense-related suspension [1]. If the error is not a promise, it indicates a standard runtime error, and the code proceeds to mark the VNode for a forced update using a helper function likemarkAsForce[1]. This ensures that when the error is caught—or if the component recovers—the system knows the component's state or render output may have been affected and requires re-processing [1]. Theoptions._catchErrorfunction is then called to allow for user-defined error boundaries to handle the exception [1]. This error handling structure allows Preact to distinguish between asynchronous suspensions (which wait for a promise to resolve) and synchronous runtime exceptions (which trigger error boundaries) [1].Citations:
🏁 Script executed:
Repository: TanStack/query
Length of output: 2438
🏁 Script executed:
Repository: TanStack/query
Length of output: 288
🏁 Script executed:
Repository: TanStack/query
Length of output: 1450
🏁 Script executed:
Repository: TanStack/query
Length of output: 4976
Normalize nullish error values before throwing.
When a query function rejects with
undefinedornull,query-corestores that value inresult.error. WiththrowOnError,getHasErrorselects the failed result anduseQueriesthrows the nullish value. Preact 10.28.0 readse.thenbefore calling the ErrorBoundary, so the render can fail with a frameworkTypeError.Normalize nullish values to an
Errorbefore throwing. Add regression coverage for both rejection reasons.🤖 Prompt for AI Agents