Skip to content

Error-prone: "Response with null body status cannot have body" #487

Closed

Description

@ConradIrwin

We were trying to stub out the fetch API using XmlHttpRequest (so that we could access progress events).

We ran into a bug because some response codes are handled specially, so now we have code that looks like this:

       if (this._xhr.status == 101 || this._xhr.status == 204 || this._xhr.status == 205 || this._xhr.status == 304) {
         body = null
       } else if (this._xhr.response) {
          body = @_xhr.response
       } ...

      new Response(body, ...)

Is there a reason for special casing these response codes? It seems a rather nasty edge-case that everyone has to handle.

Activity

  1. annevk commented on Feb 13, 2017

    @annevk
    Member

    I don't think you need to special case them in XMLHttpRequest, since XMLHttpRequest uses the same underlying primitives. If you can produce a non-null body in XMLHttpRequest with these status codes I'd love to know.

  2. ConradIrwin commented on Feb 13, 2017

    @ConradIrwin
    Author

    Hah! My mistake for not including the entire snippet:

           if (this._xhr.status == 101 || this._xhr.status == 204 || this._xhr.status == 205 || this._xhr.status == 304) {
             body = null
           } else if (this._xhr.response) {
              body = @_xhr.response
           } else {
              body = @_xhr.responseText
           }
    

    So in this case it goes wrong because the responseText is the empty string, which is not null, and it explodes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions