Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afterResponse fired 3 times on error #2324

Open
makhataibar opened this issue Jan 26, 2024 · 3 comments
Open

afterResponse fired 3 times on error #2324

makhataibar opened this issue Jan 26, 2024 · 3 comments

Comments

@makhataibar
Copy link

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.
@noxify
Copy link

noxify commented Feb 6, 2024

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
Copy link
Author

@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
Copy link

noxify commented Feb 7, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants