sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.27k stars 935 forks source link

afterResponse fired 3 times on error #2324

Open makhataibar opened 9 months ago

makhataibar commented 9 months ago

Describe the bug

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.
noxify commented 8 months ago

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,
    },
    // ....
})
makhataibar commented 8 months ago

@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();
noxify commented 8 months ago

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.