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

ERROR: Proxy connection failed - print details #29

Closed
pavelsr opened this issue Dec 21, 2019 · 3 comments
Closed

ERROR: Proxy connection failed - print details #29

pavelsr opened this issue Dec 21, 2019 · 3 comments

Comments

@pavelsr
Copy link

pavelsr commented Dec 21, 2019

Hi, Roberto. I think it would be nice to print proxy connection error more verbose.

Example.

/app # echo $https_proxy
https://51.158.186.141:8080/

/app # MOJO_PROXY=1 MOJO_CLIENT_DEBUG=1 MOJO_EVENTEMITTER_DEBUG=1 perl tg.pl
-- Blocking request (https://api.telegram.org/botXXXXXXXXXXXX/getMe)
-- Emit prepare in Mojo::UserAgent (0)
-- Emit start in Mojo::UserAgent (0)
-- Emit prepare in Mojo::UserAgent (0)
-- Emit start in Mojo::UserAgent (0)
-- Connect 9951afbd78600392637ec703384c87b1 (https://api.telegram.org:443)
-- Emit error in Mojo::IOLoop::TLS (1)
-- Emit error in Mojo::IOLoop::Client (1)
-- Emit finish in Mojo::Message::Response (0)
-- Emit finish in Mojo::Transaction::HTTP (0)
-- Emit finish in Mojo::Message::Response (0)
-- Emit finish in Mojo::Transaction::HTTP (0)
ERROR: Proxy connection failed at /usr/local/share/perl5/site_perl/WWW/Telegram/BotAPI.pm line 209.
	WWW::Telegram::BotAPI::api_request() called at /usr/local/share/perl5/site_perl/WWW/Telegram/BotAPI.pm line 76
	WWW::Telegram::BotAPI::AUTOLOAD(WWW::Telegram::BotAPI=HASH(0x55b090419f50)) called at tg.pl line 17

/app # curl -vL https://example.org/
* Uses proxy env variable https_proxy == 'https://51.158.186.141:8080/'
*   Trying 51.158.186.141:8080...
* TCP_NODELAY set
* connect to 51.158.186.141 port 8080 failed: Connection refused
* Failed to connect to 51.158.186.141 port 8080: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 51.158.186.141 port 8080: Connection refused
@Robertof
Copy link
Owner

Robertof commented Jan 8, 2020

Hi Pavel,

happy new year! I'm very sorry for the late response but it's been a busy period -- thanks for using my module.

So as you might know the entire proxy handling is done by Mojo::UserAgent, and what I do is to just print out the error that Mojo returns. You can see the lines which generate the error here:

https://github.com/mojolicious/mojo/blob/1e8ed18e79025e0e7dc1f2a50a37da6e0fb048c9/lib/Mojo/UserAgent.pm#L155-L157

I'll try to investigate this a bit further and see if I can accommodate your request, though. I'll let you know!

Robertof added a commit that referenced this issue Jan 9, 2020
This commit was made following a suggestion in #29 due to the way
Mojo::UserAgent handles proxy errors. Long story short, when a
connection to the specified proxy server fails, Mojo::UserAgent
returns an opaque "Proxy connection failed" without returning
the real reason of failure. This can be problematic when debugging,
thus now this module extracts the real failure reason in debug mode.

From a technical standpoint, this attaches an on "start" handler on
the main `UserAgent` object which is invoked for each connection attempt
made by the user agent. This includes `CONNECT` requests, and when that
happens the module attaches an on "finish" handler to that connection
which allows to extract the real failure reason.
@Robertof
Copy link
Owner

Robertof commented Jan 9, 2020

Hey! I got great news.

I was able to find a way to intercept the real reason of failure for proxy errors and I have pushed 2f6a791 (which will soon be in version 0.12 on CPAN) which will print these kind of failures when using debug mode (i.e. TELEGRAM_BOTAPI_DEBUG=1).

If you need the actual proxy failure in code and not when debugging, then you can retrieve the Mojo::UserAgent instance from the module and perform the same process that I'm doing. It would look like:

# assuming $api is a WWW::Telegram::BotAPI object
my $agent = $api->agent;
if ($agent->isa ('Mojo::UserAgent')) { # Only related to Mojo::UserAgent
    $agent->on (start => sub {
        my (undef, $tx) = @_;
        # Skip all requests which are not proxy-related.
        return unless $tx->req->method eq "CONNECT";
        # Add an handler on completion.
        $tx->on (finish => sub {
            my $tx = shift;
            if (my $error = $tx->error) {
                # $error is an hash reference with "message" in it. Do whatever you need with that information.
                say "Kaboom! ", $error->{message};
            }
        });
    })
}

Hope this has been useful. Feel free to reopen the issue if anything else comes up!

Have a good day,
Roberto

@Robertof Robertof closed this as completed Jan 9, 2020
@pavelsr
Copy link
Author

pavelsr commented Jan 9, 2020

Great, Roberto, thanks so much!

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