Skip to content
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

Errors have undefined body when using streams #1138

Closed
2 tasks done
adityapatadia opened this issue Mar 30, 2020 · 10 comments · Fixed by #1051
Closed
2 tasks done

Errors have undefined body when using streams #1138

adityapatadia opened this issue Mar 30, 2020 · 10 comments · Fixed by #1051
Labels
documentation The issue will improve the docs enhancement This change will extend Got features

Comments

@adityapatadia
Copy link

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.
@szmarczak szmarczak added the documentation The issue will improve the docs label Mar 30, 2020
@szmarczak
Copy link
Collaborator

This has been also fixed in #1051

@szmarczak szmarczak added the enhancement This change will extend Got features label Mar 30, 2020
@szmarczak
Copy link
Collaborator

You need to access the response via error.response, the docs are outdated

szmarczak added a commit to szmarczak/got that referenced this issue Apr 1, 2020
@adityapatadia
Copy link
Author

Is this going to be consistent in future versions? Or is it going to change in #1051 ?

@szmarczak szmarczak mentioned this issue Apr 1, 2020
18 tasks
@szmarczak szmarczak changed the title Error event has undefined body when using streams Errors have undefined body when using streams Apr 1, 2020
@szmarczak
Copy link
Collaborator

I don't know exactly what do you mean, but this issue is fixed in #1051.

@adityapatadia
Copy link
Author

I meant currently err.response works. My question is will the function be .on("error", (err, body, response)= {}) or it will be .on("error", (err)= {}) ? Are you going to update the docs with the existing functionality or you are going to give functionality as per current docs?

I am asking because if it's going to change, we will also need to change code.

@szmarczak
Copy link
Collaborator

.on('error', error => { ... })

@adityapatadia
Copy link
Author

Cool. I will disregard the docs then. I am doing coding as per err.response now.

@adityapatadia
Copy link
Author

adityapatadia commented Apr 1, 2020

Btw, I found another bug. Even if decompress:true is passed, the error.response.body is not decompressed if it's a gzip from the server. Should I report that one?

@szmarczak
Copy link
Collaborator

szmarczak commented Apr 1, 2020

yes, it'd be best to include some reproducible code (but I think I can sketch something on my own so that's not necessary, but it will be welcome)

@adityapatadia
Copy link
Author

Opened #1142

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation The issue will improve the docs enhancement This change will extend Got features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants