-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Throw error when ky.stop
is returned
#310
Conversation
ky.stop
is returned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Maybe we need to add a line to make sure that the error contains 400 status code.
@@ -500,7 +500,7 @@ test('beforeRetry hook can cancel retries by returning `stop`', async t => { | |||
} | |||
}); | |||
|
|||
await ky.get(server.url, { | |||
await t.throwsAsync(ky.get(server.url, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add assertions about the thrown error. Otherwise, for all we know Ky is experiencing some kind of internal error.
This change makes sense to me, in so far as I believe that throwing is probably the behavior people generally want when stopping retries. But looking at it more, it makes me question the value of ky.get('http://example.com', {
hooks: {
beforeRetry: [
({error}) => {
if (someCondition) {
throw error; // <-- stops retries and short-circuits beforeRetry loop
}
}
]
}
}); The above has always been possible, actually. But there's no way for users to short-circuit the So, perhaps this is really a documentation issue and we should encourage users to |
I agree with @sholladay |
I don't quite agree with such solution. ky.get('https://example.com', { beforeRetry: [() => ky.stop] })
.json()
.then(data => console.log(data.property)) So do you suggest that in this simple case, if retry is triggered and
This behavior is preferable in the first place because it lets users replace So yes, there are situations when Considering this, I think it would be better either to remove |
I'm not sure what you mean. Are you saying that you want to remove
Well, I think it's important to keep in mind that not everyone is using body method shortcuts (e.g. For people who are using body method shortcuts, like in your example, an error will be thrown because there's no body to parse (in fact, even before then, Ky actually runs into a |
I agree too. |
Well, it actually seems that we need to make some tweaks for those as well. Like here: ky.put("https://httpstat.us/500/cors", {
hooks: { beforeRetry: [() => ky.stop] }
})
.then((res) => console.log(res.ok)) If
Now that would probably be silly, since someone may be using it, so another option I wrote at the end of my comment: leave it as syntactic sugar for Also leaving |
Thanks for clarifying. Your points make sense to me but my stance is still that
I disagree. Whether it throws an error or not, the whole purpose of For example, this would run successfully without a property access error because of the use of optional chaining: ky.put("https://httpstat.us/500/cors", {
hooks: { beforeRetry: [() => ky.stop] }
})
.then((res) => console.log(res?.ok)) Another thing to consider is that I've personally written code where Ky is merely a fire-and-forget side effect and I never use the response. An example is when The way I'm thinking about this is that In other words, when a user decides to give up on retrying, I think they ought to have options regarding whether the operation is considered successful or unsuccessful. Right now, |
Thinking about this way, it makes sense, thanks for clarification. Well, after thinking about this, I think I agree with this solution. But there must be a big warning in readme that says |
For now, let's update the code examples to use |
Closing in favor of #314 |
Fixes #290. This also may be a breaking change although no one seems to have opened an issue that returning
ky.stop
causesTypeError
until now.