Created
July 24, 2017 20:26
-
-
Save ghandmann/f180bfa53576433f3eaae0d0244097b3 to your computer and use it in GitHub Desktop.
Mojo::UserAgent async get request to unknown host error handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojo::Base -strict; | |
use Mojolicious; | |
use Mojo::UserAgent; | |
use Try::Tiny; | |
say "Mojolicious Version $Mojolicious::VERSION"; | |
my $ua = Mojo::UserAgent->new(); | |
my $requestCallback = sub { | |
my ($ua, $tx) = @_; | |
my $res = $tx->result; | |
if($res->is_success) { | |
say $res->body; | |
} | |
else { | |
say "Request Failed: " . $res->code() . " " . $res->message(); | |
} | |
Mojo::IOLoop->stop; | |
}; | |
try { | |
$ua->get("https://invalid-host-name.me/", $requestCallback); | |
} | |
catch { | |
say "Catched: $_"; | |
Mojo::IOLoop->stop; | |
}; | |
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of the unresolvable name triggering the catch block, i only receive a warning from the Mojo::Reactor.