got icon indicating copy to clipboard operation
got copied to clipboard

afterResponse fired 3 times on error

Open makhataibar opened this issue 1 year ago • 3 comments

Describe the bug

  • Node.js version:
  • OS & version:

Actual behavior

When got throwing errors afterResponse hook fired 3 times

Expected behavior

afterResponse should be fired only once

Code to reproduce

http.extend({
                hooks: {
                    beforeRequest: [
                        options => {
                           
                                console.log(
                                    'Request: %s %s',
                                    options.method,
                                    options.url?.toString(),
                                );
                            
                        },
                    ],
                    afterResponse: [
                        response => {
                            const pathname = response.requestUrl.pathname;
                            const method = response.request.options.method;
                            const statusCode = response.statusCode;


                            console.log(
                                'Response: %s %s %s %s',
                                method,
                                statusCode,
                                pathname,
                            );


                            return response;
                        },
                    ],
                },
            }) as HttpClient;```

<!--
We encourage you to submit a pull request with a failing test:
 - This will make it more likely for us to prioritize your issue.
 - It's a good way to prove that the issue is related to Got and not your code. 

Example: https://github.com/avajs/ava/blob/master/docs/01-writing-tests.md#failing-tests
-->

#### Checklist

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

makhataibar avatar Jan 26 '24 12:01 makhataibar

Works as expected.

Since got retries on error by default, the hook is triggered each time.

From the docs:

To disable this option, set options.retry.limit to 0.

Example:

await got("http://localhost:3000/noretry", {
    retry: {
      limit: 0,
    },
    // ....
})

noxify avatar Feb 06 '24 11:02 noxify

@noxify I had retry limit in extended instance

container
        .bind<HttpClient>(TYPES.HttpClient)
        .toDynamicValue(context => {

            return got.extend({
                https: {
                },
                hooks: {
                    beforeRequest: [
                        options => {
                           
                        },
                    ],
                },
                retry: {
                    limit: 0,
                },
            }) as HttpClient;
        })
        .inSingletonScope()
        .whenTargetIsDefault();

makhataibar avatar Feb 06 '24 16:02 makhataibar

Hi @makhataibar, unfortunately I can't reproduce your described behavior.

here the repo which I have used to reproduce it: https://github.com/noxify/honojs-got And here the link to the test client for your case: https://github.com/noxify/honojs-got/blob/main/src/client-extend.ts

Could you please provide a repo which includes a running example which reproduce your issue.

noxify avatar Feb 07 '24 14:02 noxify