question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expire errored payload with `suspense: true`

See original GitHub issue

I have an error boundary with a retry button that updates its own key to rerender the whole subtree. I want to retry the failed request on rerender (I use shouldRetryOnError: false).

I tried to rethrow the error around useSWR. I get the logs, but on next render, the xhr request isn’t refired, but SWR reraises the same error as before 😦

  try {
    console.log("requesting", { key });
    result = useSWR(key, fetcher, { suspense: true });
  } catch (err) {
    console.log("error", { key });
    trigger(key);
    throw err;
  }

  const { data, isValidating, revalidate } = result;

Any pointer?

Thx a lot!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
nulladdictcommented, Mar 4, 2020

Hi @bbenezech, I think the better approach is to clear out cached error in designated error boundary component Here’s a codesandbox example of approach I’ve been using

Couple points to mention:

  • if we caught a promise we’re just need to re-throw it since it’s a suspender
  • if we caught an actual error we save info about what key caused it, so we can clear cache entry later
  • in ErrorGuard component we clear cached entry when user clicks the button, but we could also just clear it in setState callback inside componentDidCatch method instead
  • we can also clear out all cached errors if we iterate over cached keys and check if they’re error or not, but that seems kinda dangerous to me
1reaction
nulladdictcommented, Feb 25, 2020

Hi, since #231 got merged you can now clear up the cached error value on your own. For your example it might look something like this:

import { cache } from 'swr';
// ...
try {
  console.log("requesting", { key });
  result = useSWR(key, fetcher, { suspense: true });
} catch (err) {
  console.log("error", { key });
  const [, , errorKey] = cache.serializeKey(key);
  cache.delete(errorKey, false);
  throw err;
}
// ...
const { data, isValidating, revalidate } = result;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Expire errored payload with suspense: true #160 - GitHub
I have an error boundary with a retry button that updates its own key to rerender the whole subtree. I want to retry...
Read more >
NextJS not mounting page content on Server Side even using ...
I used useEffect as a trigger, capturing the page's mount event and triggering the function to perform the lazy import, passing the payload...
Read more >
Order Completed - DriveWealth Developer Portal
Results Explained ; payload.orderExpires, string, the timestamp of when the order expires ; payload.createdBy, string, userID of who created the order ; payload....
Read more >
makeRenderRestHook() - Rest Hooks
readonly error: Error; ... return useSuspense(ArticleResource.get, payload); }); expect(result.current).toBeUndefined(); ... toBe(true);
Read more >
Oracle Financials Cloud 20D What's New
However, the opt-in for this feature will expire in 21A. ... accommodation expenses for policy violations and displays the warning and error messages...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found