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

exclude fetchApi "Failed to fetch" from ignores #37

Closed
oreporan opened this issue Nov 18, 2020 · 5 comments · Fixed by #38
Closed

exclude fetchApi "Failed to fetch" from ignores #37

oreporan opened this issue Nov 18, 2020 · 5 comments · Fixed by #38

Comments

@oreporan
Copy link
Contributor

oreporan commented Nov 18, 2020

Currently, as the API states, you discard instance of TypeError, however, the fetchApi Failed to fetch exception is of type TypeError.
One of the common reasons that this error occurs can be network connection issues.
Is it possible to include this exception?
The main reason I want to add a retry mechanism is incase the user had a bad internet connection during that second where the request was going out

From Fetch documentation:

A fetch() promise will reject with a TypeError when a network error is encountered or CORS is misconfigured on the server-side

Current workaround:

 pRetry(async () => {
        try {
            return await fetchRequest()
        } catch (error) {
            if (error instanceof TypeError && error.message === 'Failed to fetch') {
                throw new Error('Failed to fetch')
            }
            throw error;
        }
    }, {
        retries: 2,
        onFailedAttempt,
    });

Thanks

@sindresorhus
Copy link
Owner

Who thought it was a good idea for Fetch to throw a TypeError on network errors...


Slightly relevant issue: whatwg/fetch#526

@sindresorhus
Copy link
Owner

But yes, we should exclude this. Also needs to be documented.

@oreporan
Copy link
Contributor Author

I'm also happy to put a PR in place

@sindresorhus
Copy link
Owner

Yes. PR welcome :)

@chenliu0831
Copy link

+1, this is a bit unexpected when people use retry to mitigate network errors from fetch

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 a pull request may close this issue.

3 participants