Skip to content

Instantly share code, notes, and snippets.

@ghandmann
Created July 24, 2017 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghandmann/f180bfa53576433f3eaae0d0244097b3 to your computer and use it in GitHub Desktop.
Save ghandmann/f180bfa53576433f3eaae0d0244097b3 to your computer and use it in GitHub Desktop.
Mojo::UserAgent async get request to unknown host error handling
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;
@ghandmann
Copy link
Author

Instead of the unresolvable name triggering the catch block, i only receive a warning from the Mojo::Reactor.

$ perl test.pl
Mojolicious Version 7.37
Mojo::Reactor::Poll: Timer failed: Can't connect: Name or service not known at test.pl line 12.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment