Created
September 14, 2021 04:50
-
-
Save michaelfung/0ab0624f53d3f5b101e677276179b6ba to your computer and use it in GitHub Desktop.
test case for unhandled promise warning
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, -async_await; | |
use Test::More; | |
use Test::Mojo; | |
use Mojolicious::Lite; | |
# this gives unhandled promise warning: | |
helper get_site_title => async sub { | |
my ($c, $url) = @_; | |
my $result = (await $c->ua->get_p($url))->result; | |
die $result->message if $result->is_error; | |
die 'unknown error condition' unless $result->is_success; | |
return $result->dom->at('title')->text; | |
}; | |
helper get_site_title2 => async sub { | |
my ($c, $url) = @_; | |
my ($tx, $err); | |
eval { $tx = await $c->ua->get_p($url)->catch( sub { ($err) = @_; });}; | |
die "$err" if $err; | |
my $result = $tx->result; | |
die $result->message if $result->is_error; | |
die 'unknown error condition' unless $result->is_success; | |
return $result->dom->at('title')->text; | |
}; | |
get '/title' => async sub { | |
my ($c) = @_; | |
$c->render_later; | |
my $title ='failed to get title'; | |
eval { $title = await $c->get_site_title($c->param('url'))}; | |
$title .= ", error: $@" if $@; | |
$c->render(text => "$title"); | |
}; | |
get '/title2' => async sub { | |
my ($c) = @_; | |
$c->render_later; | |
my $title ='failed to get title'; | |
eval { $title = await $c->get_site_title2($c->param('url')) }; | |
$title .= ", error: $@" if $@; | |
$c->render(text => "$title"); | |
}; | |
my $t = Test::Mojo->new; | |
$t->get_ok('/title' => form => { url => 'http://badsite.lan/'}) | |
->content_like(qr/failed/i); | |
$t->get_ok('/title2' => form => { url => 'http://badsite.lan/'}) | |
->content_like(qr/failed/i); | |
done_testing(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://gist.github.com/kiwiroy/137269c99050e90887262fbae1b1113e