Handeling UserAgent / DOM errors #2063
Answered
by
kraih
hussam-qasem
asked this question in
Q&A
-
my $ua = Mojo::UserAgent->new;
$ua->proxy->https('http://127.0.0.1:3128');
my $tx = $ua->get( "https://www.mojolicious.org/index.html" );
die $tx->result->message unless $tx->result->is_success; produces
eval {
say STDOUT "name: ",
Mojo::DOM->new
->parse( '<tr><td>Username:</td><td><input type="text" Xname="g3x761111" /></td></tr>' )
->at( 'input[type="text"][name^="g3"]' )->attr->{name};
};
if ($@) {
warn "Error accessing input element: $@";
} vs. my $dom = Mojo::DOM->new
->parse( '<tr><td>Username:</td><td><input type="text" Xname="g3x761111" /></td></tr>' )
->at( 'input[type="text"][name^="g3"]' );
say STDOUT "name: ", $dom->attr->{name} if $dom;
warn "Error accessing input element" unless $dom; |
Beta Was this translation helpful? Give feedback.
Answered by
kraih
Apr 27, 2023
Replies: 1 comment 4 replies
-
So I figured one part. It seems accessing the my $ua = Mojo::UserAgent->new;
$ua->proxy->https('http://127.0.0.1:3128');
my $tx = $ua->get( "https://www.mojolicious.org/index.html" );
my $result = eval { $tx->result };
die "Error occurred: $@" if $@;
die $result->code."/".$result->message unless $result->is_success; |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The whole point
->result
exists is to throw an exception. If you don't want that you could just call->res
instead and check manually for errors.