-
Notifications
You must be signed in to change notification settings - Fork 0
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
Update fetcher.ts to work like the browser #66
Conversation
I'm trying to use `if(request.status == 400) { ...` but the `if !request.ok then throw` was preventing me from doing it
@juanmahidalgo @aleortega if you are OK with the approach, I'll correct the tests. With retries, I'd return the last try's response |
const responseText = await response?.text() | ||
throw new Error(`Failed to fetch ${url}. Got status ${response?.status}. Response was '${responseText}'`) | ||
} | ||
|
||
if (!isUsingNode() && !!response) { |
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.
if (!isUsingNode() && !!response) { | |
if (!isUsingNode() && response?.ok) { |
do we need to add response?.ok
here so it creates the buffer just if response.ok
is truthy?
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.
I think the buffer should always be available. 400 and 500 errors also have response bodies
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.
yeah, true, they should return the buffer as well. LGTM then!!
Hello, looks good to me as well, in fact this was my first approach back then but received a feedback from the team saying that they preferred to throw in case of Anyways, this is not a strong opinion but may this change will bring us the versatility to opt-in/out the preferred approach. |
I think it is a ..weird behavior, I was trying to get the error message and fields from the 401 response; and instead I got a hard to parse exception message. In the long term, I think the best is not to assume that this "throw" is a good thing and do server code as we do in the front-end, checking for the response.ok every time depending on the use case. That way anyone can leverage their knowledge about how "fetch" works, without any surprise bound to this implementation. Otherwise, I'd advice to name the function differently to "fetch" following the maxima of functional programming "if the function does, receives or returns something different, it should be named differently". |
Got it, so I'm ok with merging this. I'll update the code using this fetch to adapt it to the new implementation. Thanks for the heads-up! |
@menduz are you going to work on the tests? I want to integrate this module but I would like to have this merged first, thanks! |
I had to shift priorities, do you want to take it? |
I have already updated the tests weeks ago, but also had to shift priorities. Let me see if I can recover, adapt and push that commit today. |
Hey @menduz @hugoArregui, I've just pushed the tests. I had already adapted these tests in the After merging this PR I'm deleting the mentioned branch. |
In case you're curious about the other approach, here is the PR. |
thanks for the context, the tests and the experiment Ale! I also think it looks cleaner without the opt-in. I think we are go for mering and releasing a new major. le damos? |
Yass, all yours :) I'll let the team know about the change and start upgrading some services where I can use this (eager to remove those try/catches). |
This PR removes a forceful throw and
return undefined
to copy the default behavior of the browser.Context
I'm trying to use
but the
was preventing me from doing it