You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To workaround the problem, I created this function which makes sure no error is bubbling up:
import{mutate}from"swr";functionsafeMutate(key,action,revalidate){returnnewPromise((resolve,reject)=>{mutate(key,(oldValue)=>{returnaction(oldValue).catch((error)=>{reject(error);!revalidate&&mutate(key,null);// wont work without null});},revalidate).then(resolve);});}
One drawback is that when setting revalidate to false and an error is thrown, we need to mutate to null to force revalidation (see comment "wont work without null").
Thank you for opening this! This is currently by design, but I think we can improve it so we can differentiate "fetch" errors and "mutate" errors (haven't found a good design for it yet). For the same reason, we need the data and isValidating states to be context-aware (related to #1316). One way to simplify all these is using the upcoming useSWRMutation feature (#1450).
In this scenario, opts is undefined, so SWR encounters an error on !opts.skip. Enabling the ts null check flag would've likely caught it, but it's not enabled in this codebase.
When SWR encounters this error, it stays silent. the returned error stays undefined.
@dangdennis That might not be related to this thread, but did you mean conditional fetching (the key function throws an error, which pauses the request).
Bug report
Description / Observed Behavior
When calling mutate with a function as 2nd argument and an error is thrown inside that function, the error will bubble up to
error
returned by useSWR.Expected Behavior
The error should not bubble up, as it mixes up data fetching with data mutation errors.
As written in the API Options
Correct would be
error thrown by fetcher or inside mutate (or undefined)
I don't know if this is an intentional design decision or just a bug.
Repro Steps / Code Example
I created two sandboxes:
To workaround the problem, I created this function which makes sure no error is bubbling up:
One drawback is that when setting
revalidate
tofalse
and an error is thrown, we need to mutate to null to force revalidation (see comment "wont work without null").As described in the docs
In fact, we need to revalidate at least when there is an error to make it go away.
Additional Context
SWR version 1.0.1
The text was updated successfully, but these errors were encountered: