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

[feature request] add hook onNetworkError #296

Open
xialvjun opened this issue Nov 4, 2020 · 8 comments
Open

[feature request] add hook onNetworkError #296

xialvjun opened this issue Nov 4, 2020 · 8 comments

Comments

@xialvjun
Copy link

xialvjun commented Nov 4, 2020

just as the title
because afterResponse is not invoked when there is a network error.

@duongvanba
Copy link

Are there any fix ?

@szmarczak
Copy link
Collaborator

Are there any fix ?

No, there is not; I don't think commenting "is there any fix" will bring this feature forward. A PR is always welcome :)

Maybe a generic hook for all errors would be better? Like we do in Got.

@duongvanba
Copy link

Thank for your reply
I'm implement de-duplicate concurrent request in my react appliction
I have a custom hook that use Ky to fetch api from server, but I use this hook many places, so I just implement a Ky hook to de-duplicate request
But i have problem when detect first request status on network error. There is no way to use Ky hooks to detect first request network error to reject a Promise for other reques
Anw, i see that Ky is awesome package
I come here from Got, I love Got and I really nodejs support HTTP2 normaly that we can use http2 as default in future project
Thank you so much for quick reply

@szmarczak
Copy link
Collaborator

But i have problem when detect first request status on network error. There is no way to use Ky hooks to detect first request network error to reject a Promise for other reques

The solution is pretty simple. You need to store an array of abort controllers, and if the first request fails (you can use promise.catch()), call all of them.

@sholladay
Copy link
Collaborator

sholladay commented Dec 4, 2020

As far as I know, there hasn't been any progress at WHATWG related to discrete error types for fetch, which is what we would need to do this properly.

I think probably the best we can do in Ky, at least for now, is to act like any non-HTTP error is a network error. We could provide a hook for that. It would end up getting called for invalid fetch options too, not just network errors. That might be confusing. But it's doable.

@szmarczak
Copy link
Collaborator

That might be confusing. But it's doable.

Not necessarily confusing, it would be pretty useful when attaching a logger.

@duongvanba
Copy link

duongvanba commented Dec 5, 2020 via email

@MichaelSitter
Copy link

MichaelSitter commented Jul 23, 2021

Struggled with this for the better part of a day, I'll leave this as a potential solution in case its useful. In my case I have a reusable client with circuit breaking which needs to know about timeouts & network errors, in addition to HTTP errors. Providing a wrapped fetch option at least gave me a way to observe those errors outside of hooks.

ky.extend({
  fetch: (input: RequestInfo, init?: RequestInit): Promise<Response> => {
      const fn = opts.fetch || globalThis.fetch.bind(globalThis)
      return fn(input, init)
        .catch(err => {
          // observe error as needed
          throw err
        })
  }
})

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

No branches or pull requests

5 participants