[feature request] add hook onNetworkError
just as the title
because afterResponse is not invoked when there is a network error.
Are there any fix ?
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.
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
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.
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.
That might be confusing. But it's doable.
Not necessarily confusing, it would be pretty useful when attaching a logger.
Thank you so much
Vào Th 6, 4 thg 12, 2020 vào lúc 17:18 Szymon Marczak < [email protected]> đã viết:
That might be confusing. But it's doable.
Not necessarily confusing, it would be pretty useful when attaching a logger.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sindresorhus/ky/issues/296#issuecomment-738700193, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGLTBEJI4EC7CVLN2N2SJ7DSTCZQZANCNFSM4TKBELZQ .
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
})
}
})