Skip to content

Exception in catch block: TypeError: Response.text: Body has already been consumed. #1544

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

Closed
1 task
tgross35 opened this issue Feb 16, 2024 · 2 comments · Fixed by #1546
Closed
1 task
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-fetch Relevant to the openapi-fetch library PRs welcome PRs are welcome to solve this issue!

Comments

@tgross35
Copy link

tgross35 commented Feb 16, 2024

Description

If the error response is not JSON, this will raise a type error rather than returning the error body text.

Reproduction

Use the fetch API to hit an endpoint that returns a 4xx response with invalid JSON (i.e. plaintext). Based on debugging, the following is the issue:

    // handle errors (always parse as .json() or .text())
    let error = {};
    try {
      error = await response.json();
    } catch {
      error = await response.text();
    }

await response.json(); seems to first consume the body, then try parsing JSON. If reading the body succeeds but parsing the JSON fails, then the catch block will hit and response.text() will fail (since the body has already been consumed).

Expected result

error should probably still contain the error text, even if the response is non-JSON. I believe that the below would fix this:

    const text = await response.text();
    try {
      error = JSON.parse(text);
    } catch {
      error = text;
    }

Checklist

@tgross35 tgross35 added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Feb 16, 2024
@drwpow drwpow added PRs welcome PRs are welcome to solve this issue! good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project labels Feb 16, 2024
@drwpow
Copy link
Contributor

drwpow commented Feb 16, 2024

Great catch! We had tests for unexpected responses for data, but not for error.

This would normally be a great issue for someone to pick up, but given this could break some sites and is a regression from the 0.9.0 version released earlier today, probably better to patch it sooner than later.

@tgross35
Copy link
Author

Thanks for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-fetch Relevant to the openapi-fetch library PRs welcome PRs are welcome to solve this issue!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants