Got: `HTTPError`s have unspecified response body

Created on 20 Apr 2020  路  12Comments  路  Source: sindresorhus/got

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

  • [x] I have read the documentation.
  • [x] I have tried my code with the latest version of Node.js and Got.
bug regression

All 12 comments

Can you set up a full example of the bug?

We have many tests that prove it's defined, for example: https://github.com/sindresorhus/got/blob/1f6ac4597b797e6fe760a7dc11a3db8bf298aa94/test/error.ts#L20

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

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.

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

https://github.com/sindresorhus/got/blob/1f6ac4597b797e6fe760a7dc11a3db8bf298aa94/test/error.ts#L20

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..

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.

The HTTPError should always have a .response property.

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

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

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

Fixed in d914a7e7a0d97b03745b9b47623e4ac3532b4dae

Released 11.0.1, thanks for reporting!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alanzhaonys picture alanzhaonys  路  4Comments

astoilkov picture astoilkov  路  3Comments

carvallegro picture carvallegro  路  4Comments

tkoelpin picture tkoelpin  路  3Comments

framerate picture framerate  路  4Comments