Skip to content

Change "Body is unusable" error to something more helpful #3097

@nzakas

Description

@nzakas

This would solve...

Unidici throws an error with the message "Body is unusable" when the body of a response is non-null and the stream is either locked or disturbed. In most cases, this situation occurs because someone has attempted to ready the body twice, such as:

const response = await fetch(url);
console.log(await response.text());
const body = await response.json();  // throws "Body is unusable"

The message "Body is unusable" is opaque and requires searching to understand what it means and what the problem is. A more descriptive message would save developers a lot of time.

The implementation should look like...

Changing the string on this line:

throw new TypeError('Body is unusable')

Suggestion: "Body is unusable: Body is either null or the stream has already been read."

The spec only specifies that a TypeError must be thrown, but it does not specify the text contained within that error, so it seems like changing the message would still be in-line with the spec.

I have also considered...

Not filing this issue. 😄

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions