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

How to catch "Failed to handle request" error? #78

Open
OnkelTem opened this issue May 11, 2022 · 5 comments
Open

How to catch "Failed to handle request" error? #78

OnkelTem opened this issue May 11, 2022 · 5 comments

Comments

@OnkelTem
Copy link

OnkelTem commented May 11, 2022

My proxy code is as follows:

server.forUnmatchedRequest().thenPassThrough({});

And I get lots of messages like this in the console:

Failed to handle request: getaddrinfo ENOTFOUND ton.local.twitter.com
Failed to handle request: getaddrinfo ENOTFOUND ton.local.twitter.com
Failed to handle request: getaddrinfo ENOTFOUND ton.local.twitter.com
Failed to handle request: getaddrinfo ENOTFOUND ton.local.twitter.com
Failed to handle request: getaddrinfo ENOTFOUND ton.local.twitter.com
Failed to handle request: getaddrinfo ENOTFOUND ton.local.twitter.com
Failed to handle request: getaddrinfo ENOTFOUND ton.local.twitter.com
Failed to handle request: getaddrinfo ENOTFOUND ton.local.twitter.com
Failed to handle request: getaddrinfo ENOTFOUND ton.local.twitter.com

I tried beyond a shadow of a doubt this:

server.forUnmatchedRequest().thenPassThrough({}).catch((e) => {
      console.log("ERROR", e);
});

but apparently it didn't work.

So is there a way to intercept errors like this?

@pimterry
Copy link
Member

Ok - when you say 'intercept', what would you actually like to do with the error?

Do you want to change what the HTTP error response is, or change the DNS to make that name resolve, or do you just want to log the error somewhere else?

@OnkelTem
Copy link
Author

I would just want to dismiss it, as it spoils the output :)

@pimterry
Copy link
Member

Ok, there's no general solution to this right now. The code is here, I guess we could add an .on('handler-error', (err) => ...) event to catch that, and then silence the default logging if one of those is registered - (PRs welcome!).

If these are expected errors though, I think the better solution though is to explicitly handle the request yourself. You can silence this and explicit define what should happen instead by adding a rule like:

server.forAnyRequest().forHostname('ton.local.twitter.com').always().thenReply(503);

That'll ensure this request is always explicitly rejected, and so it'll avoid the error when trying to proxy this unproxyable request. Does that work for your case?

@rusprice
Copy link

hi, @pimterry.

I'm getting the same issue

Failed to handle request: getaddrinfo ENOTFOUND api-quiz.hype.space

This subdomain doesn't exist. How can I make the DNS resolve (literally to anything that will let me modify responses afterwards)?

Example code that I'd like to be able to run:

mockServer.forPost("https://api-quiz.hype.space/verifications/0").thenCallback((req) => {
    return {
      body: { auth: null },
    };
});

Thanks for your help!

@pimterry
Copy link
Member

pimterry commented Jan 2, 2024

Hi @rusprice, I'm not sure I understand - you can do almost exactly what you're doing in that example already today. The only tiny issue there is that body should be a buffer or string (if you want automatic JSON handling, set json instead).

As long as you don't use a passthrough rule, you shouldn't see these host not found errors. Those will only appear when Mockttp attempts to passthrough the request to an upstream server, so any kind of rule that instead directly handles the request should be fine.

Does that make sense? If that doesn't work for you, it would be helpful if you could set up a full standalone example to demonstrate the issue you're seeing.

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

3 participants