Skip to content
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

Handle fetch top-level errors #618

Merged

Conversation

valeriangalliat
Copy link
Contributor

@valeriangalliat valeriangalliat commented Feb 28, 2023

Diff best viewed in ignore whitespaces mode.

Handle fetch top-level errors

This PR wraps the await fetch calls to login and logout API endpoints by a try/catch block so that we call onLoginRequestError and onLogoutRequestError not-only in case of HTTP errors, but also in case of network errors (e.g. fetch rejecting with TypeError: failed to fetch), instead of leaving those unhandled.

Retry network errors

Moved to its own PR

Also in order to mitigate network errors, this PR introduces a retry logic when calling the login and logout API endpoints, provided by fetch-retry.

This module defaults to retrying up to 3 times at 1s intervals, and only retrying network errors (the case where fetch rejects, as opposed to HTTP errors where it resolves with response.ok = false).

I think those defaults are sensible, and I added a configuration option fetchRetryConfig to allow fine tuning the retry logic if needed.


I think the rate of fetch network errors I'm seeing on login API calls is exacerbated by the fact the login API is called on every page (#424 - I tried coming up with a solution for that too but didn't find a reliable way to do that just yet), nevertheless this PR shouldn't leave those errors unhandled, and thanks to the retry logic, should lower their frequency.

Let me know what you think and if you need me to make any edits!

@vercel
Copy link

vercel bot commented Feb 28, 2023

@valeriangalliat is attempting to deploy a commit to the Gladly Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Mar 1, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
nfa-example ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 9, 2023 at 8:15PM (UTC)

@kmjennison
Copy link
Contributor

@valeriangalliat Thanks for the PR!

The try/catch behavior makes sense and would be good to merge. Could you break that into its own PR?

I don't think we should add fetch-retry as a dependency, given that developers might prefer other fetch implementations and it'll add to bundle size. Instead, I see two alternatives:

  1. Developers should define the global fetch to have desired behavior. There was a Next.js issue to build in fetch retries, though I'm not sure what the outcome was. It's also not clear to me how easy it is to change the default fetch polyfill (related issue). If anybody has any suggestions on best practices for modifying global fetch, please chime in.
  2. This library could take a fetcher function as an input. E.g., how SWR does this: https://swr.vercel.app/docs/data-fetching#fetch

@kmjennison kmjennison self-requested a review March 1, 2023 16:28
Copy link
Contributor

@kmjennison kmjennison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please break out the try/catch changes into its own PR, excluding the addition of fetch-retry.

@valeriangalliat
Copy link
Contributor Author

That makes sense! I like the idea of the fetcher function. I'll split the changes and make another PR for that early next week 👌

@valeriangalliat valeriangalliat force-pushed the fetch-top-level-error-and-retry branch from e43259f to 8fb6693 Compare March 8, 2023 22:06
@valeriangalliat valeriangalliat changed the title Handle fetch top-level errors and retry network errors Handle fetch top-level errors Mar 8, 2023
@valeriangalliat
Copy link
Contributor Author

@kmjennison updated! This PR now only includes catching of fetch errors

@codecov
Copy link

codecov bot commented Mar 9, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (f46d5ed) 99.25% compared to head (8fb6693) 99.25%.

Additional details and impacted files
@@           Coverage Diff           @@
##             v1.x     #618   +/-   ##
=======================================
  Coverage   99.25%   99.25%           
=======================================
  Files          30       30           
  Lines         673      675    +2     
  Branches      223      223           
=======================================
+ Hits          668      670    +2     
  Misses          5        5           
Impacted Files Coverage Δ
src/useFirebaseUser.js 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@kmjennison kmjennison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you!

@kmjennison kmjennison merged commit 425e89d into gladly-team:v1.x Mar 9, 2023
@valeriangalliat valeriangalliat deleted the fetch-top-level-error-and-retry branch March 9, 2023 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants