-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
error thrown in mutate bubbles up to error of useSWR hook #1551
Comments
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 I'll mark this issue as "discussion". |
I just discovered the SWR 1.1.0 also swallows null errors. https://codesandbox.io/s/cocky-smoke-kv4ir?file=/src/App.tsx In this scenario, When SWR encounters this error, it stays silent. the returned
Is this expected behavior? |
@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). |
Hah, actually you’re very right. I forgot that a thrown exception are treated the same as nulls. |
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: