Fix handling of network errors for beforeRetry
hook
#276
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #251
Fixes #275
The PR fixes some problems with hooks caused by how we handle responses:
beforeRetry
hooks get called as expected in case of a network error where no response was received. Previously, if a network error occurred, Ky would attempt to clone a non-existent response, causing an exception immediately before thebeforeRetry
hook, meaning it wouldn't be called. The simplest fix, which I've implemented here, is to just remove theresponse
parameter (which was merely a shortcut) and make people useerror.response
instead, which IMO is actually a bit clearer anyway, in that it is obvious the response may beundefined
. There are some other possible ways to fix this... we could make it a getter, or useerror.response && error.response.clone()
. Of those two alternatives, I'd prefer a getter, in order to maintain a single source of truth for what the response is.response
received byafterResponse
hooks and theerror.response
from HTTP errors has the customparseJson
function applied to it. I've implemented a_decorateResponse()
method to achieve this, which can be used to apply the custom parser anywhere a response is cloned.