Skip to content

Errors have undefined body when using streams #1138

@adityapatadia

Description

@adityapatadia

Describe the bug

  • Node.js version: 12.6.1
  • OS & version: Macos 10.14

Actual behavior

When isStream: true is set and request results in HTTPError, the stream emits error event. The emit handler is only called with error argument and body and response arguments are undefined.

...

Expected behavior

As per the docs, the body and response arguments should be set with error event.
...

Code to reproduce

const got = require('got');

let stream = got("https://www.google.com/non-existant-path", {
  isStream: true,
  method: "GET",
  headers: {}
});

stream.on("response", (response) => {
  console.log(response.headers);
  console.log(response.statusCode)
});

stream.on("error", (err, body, response) => {
  // err is set
  console.log(err);

  // body should have been set but it's undefined
  console.log(body);

  // response should have been set but it's undefined.
  console.log(response);
});

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationThe issue will improve the docsenhancementThis change will extend Got features

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions