-
-
Notifications
You must be signed in to change notification settings - Fork 356
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
feat(fetch): avoid exception when the response body has no content #1778
feat(fetch): avoid exception when the response body has no content #1778
Conversation
packages/fetch/src/index.ts
Outdated
@@ -181,7 +181,7 @@ ${ | |||
` | |||
: `const res = await fetch(${fetchFnOptions}) | |||
|
|||
const data:${response.definition.success} = await res.json() | |||
const data:${response.definition.success} = res.body ? await res.json() : {} |
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.
Are you sure this will avoid the error? I tried it with
const responseBody = res.body ? await res.json() : {};
but it throws
Uncaught (in promise) SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
if there is no response body. Using the following will not throw any errors:
const responseBody = await res.json().catch(() => ({}));
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.
As you pointed out, this code will cause an error if the body exists but is empty.
I think I need to check it by referencing the plain text once like const text = res.text()
.
I have finalized the specifications based on discussions with everyone, so check them out. |
Status
HOLD
I'm waiting for more options to gather in #1656
Description
fix #1656
This avoids an exception that occurs when executing
res.json()
if there is no content in the response body.Related PRs
none
Todos
Steps to Test or Reproduce
you can check by sample apps