mockttp
mockttp copied to clipboard
How to catch "Failed to handle request" error?
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?
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?
I would just want to dismiss it, as it spoils the output :)
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?
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!
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.