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

HTTPErrors have unspecified response body #1162

Closed
2 tasks done
kirillgroshkov opened this issue Apr 20, 2020 · 12 comments
Closed
2 tasks done

HTTPErrors have unspecified response body #1162

kirillgroshkov opened this issue Apr 20, 2020 · 12 comments
Labels
bug Something does not work as it should regression Something does not work anymore

Comments

@kirillgroshkov
Copy link

kirillgroshkov commented Apr 20, 2020

Describe the bug

  • Node.js version: 12.16.2
  • OS & version: MacOS 10.15

Actual behavior

I had a BeforeErrorHook that in got@10 had err.response defined, so I used err.response.body to display/format error. In got@11 the same code tells me that err.response.body is undefined.

Briefly:

export function gotErrorHook(): BeforeErrorHook {
  return err => {
    console.log(err instanceof HTTPError, err.response.body)
//=> true, undefined

Expected behavior

err.response.body to be defined

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.
@szmarczak
Copy link
Collaborator

Can you set up a full example of the bug?

@szmarczak
Copy link
Collaborator

We have many tests that prove it's defined, for example:

t.truthy(error.response);

but since Got has been fully rewrited to utilize the native streams new bugs may occur.

@kirillgroshkov
Copy link
Author

Can you set up a full example of the bug?

Yep. But first - am I supposed to have err.response defined? Like, this API didn't suppose to change or anything? I noticed the change from GotError to RequestError, but both have .response attached.

@kirillgroshkov
Copy link
Author

We have many tests that prove it's defined, for example:

t.truthy(error.response);

but since Got has been fully rewrited to utilize the native streams new bugs may occur.

I see. I'll try to hack together a minimal repro.. Hold on..

@szmarczak
Copy link
Collaborator

But first - am I supposed to have err.response defined?

Yep. All RequestErrors should have a .response defined (if the underlying Node.js request emitted it) except for some invalid usage errors.

@szmarczak
Copy link
Collaborator

The HTTPError should always have a .response property.

@kirillgroshkov
Copy link
Author

Ok, classic thing, minimal repro gives me .response as defined. I probably did something wrong in my first try. But, the question propagates to err.response.body, which is undefined now and it wasn't if I run the same code against got@10. Here's the got@11 minimal repro:

https://runkit.com/kirillgroshkov/5e9dbf8b5ca48500131a30b8

It logs err.response.body as undefined

@kirillgroshkov
Copy link
Author

Same code with got@10: https://runkit.com/kirillgroshkov/5e9dc3019747cc001a89d84c

@szmarczak szmarczak changed the title In BeforeErrorHook err.response is undefined HTTPErrors have unspecified response body Apr 20, 2020
@szmarczak szmarczak added bug Something does not work as it should regression Something does not work anymore labels Apr 20, 2020
@szmarczak
Copy link
Collaborator

Ok, I know how to fix this, give me 15 mins.

@szmarczak
Copy link
Collaborator

szmarczak commented Apr 20, 2020

I've encountered another problem. I passed only one beforeError hook, and there are two (options.hooks.beforeError.length). Weird...

@szmarczak
Copy link
Collaborator

Fixed in d914a7e

@szmarczak
Copy link
Collaborator

Released 11.0.1, thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something does not work as it should regression Something does not work anymore
Projects
None yet
Development

No branches or pull requests

2 participants