Got: `beforeError` hook is not working for Promise

Created on 17 Apr 2019  路  3Comments  路  Source: sindresorhus/got

When using got as a promise, the new beforeError hook is not effective since the the hook is not invoked in any non-network issue (e.g. 401)

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

Here is a simple example demonstrating the bug

got(`https://api.github.com`, {
  headers: {
    Authorization: 'Bearer invalid_token'
  },
  hooks: {
    beforeError: [error => console.log('Houston, we have a problem')]
  }
});

Expected Behaviour
It should print Houston, we have a problem.

Actual Behaviour
Nothing is printed, an error is thrown directly.

Reason
The error should have been parsed by the hook before rejecting, something like
https://github.com/sindresorhus/got/blob/92b1005cc4098a71f9ac8f984b61c07180ff77e8/source/request-as-event-emitter.ts#L45-L48

But there's none for promise
https://github.com/sindresorhus/got/blob/92b1005cc4098a71f9ac8f984b61c07180ff77e8/source/as-promise.ts#L80-L85 https://github.com/sindresorhus/got/blob/92b1005cc4098a71f9ac8f984b61c07180ff77e8/source/as-promise.ts#L93-L96

bug

Most helpful comment

All 3 comments

Hey guys, is there any time window for a new release containing the fix for this?

In meantime, is there any workaround for this?

nice to see the new alpha version :tada: , but since it is an alpha version feel like I must ask:
do you plan to include the fix in a patch for v9.6?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pvdlg picture pvdlg  路  3Comments

alanzhaonys picture alanzhaonys  路  4Comments

lukechu10 picture lukechu10  路  3Comments

dAnjou picture dAnjou  路  3Comments

carvallegro picture carvallegro  路  4Comments