Got: beforeRetry hook not receiving context from options.context

Created on 1 Apr 2020  路  4Comments  路  Source: sindresorhus/got

Describe the bug

  • Node.js version: 10.14.1
  • OS & version: MacOs 10.14.6

I'm trying to attach a logger to the context so that I can log certain messages before a retry of a request is performed. The context key in the options to got looks like this:
{ context: logger }

Actual behavior

The hook beforeRetry is not getting the context from the options passed into it. options.context is {}.
...

Expected behavior

I expect the beforeRetry hook to have the context object in the options.context parameter of its functions to be the same value which I passed in: { context: logger }.

...
I changed the hook to a beforeRequest and the context was passed through successfully.

Code to reproduce for failure

                beforeRetry: [
                    (options, error, retryCount) => {
                        const { context } = options
                        console.log(context)
                        context.logger.warn('--Redacted for privacy---', {
                            method: options.method,
                            path: options.url.pathname,
                            errorName: error.name,
                            errorMessage: error.message,
                            retryCount
                        })
                    }
                ]

image

Using beforeRequest hook worked

                beforeRequest: [
                    (options) => {
                        const { context } = options
                        console.log(context)
                        context.logger.warn('--Redacted for privacy---', {
                            method: options.method,
                            path: options.url.pathname,
                            // errorName: error.name,
                            // errorMessage: error.message,
                            // retryCount
                        })
                    }
                ]

image

Checklist

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

All 4 comments

Could you set up a full RunKit example? Please check out 11.0.0-beta.1 as it has fixed many issues.

Thank you for the example, indeed this is a bug. Will work on this today.

Fixed in 0fbc3907f452fad9123d9339d1935e7a1ded297f

Was this page helpful?
0 / 5 - 0 ratings

Related issues

f-mer picture f-mer  路  4Comments

khizarsonu picture khizarsonu  路  3Comments

alvis picture alvis  路  3Comments

tkoelpin picture tkoelpin  路  3Comments

jamestalmage picture jamestalmage  路  3Comments