sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.27k stars 935 forks source link

Error response body is not decompressed for streams #1142

Closed adityapatadia closed 4 years ago

adityapatadia commented 4 years ago

Describe the bug

Actual behavior

When stream throws error, the event listener sends error object. The error.response.body should be decompressed when got.stream() is passed decompress: true option but actually, the response is gzip compressed if the server sends HTTPError with gzip encoding.

Second issue is that error.response.body is a buffer but ideally it should be stream. ...

Expected behavior

  1. Response should be decompressed inside error object
  2. The response should also be stream rather than buffer. ...

Code to reproduce

const got = require('got');

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

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

stream.on("error", (err) => {

  console.log("This is compressed:", err.response.body); // this should be decompressed.

  console.log(Buffer.isBuffer(err.response.body)); // response.body is a buffer but ideally that should also be stream.
});

Checklist

szmarczak commented 4 years ago

Fixed in #1051 Test: bf0b0cde07b6b65b18601aa3f43f976f4150c1f2