Skip to content

Commit 4e511d0

Browse files
authored
Merge pull request #1992 from mojolicious/jhthorsen/async-await-at-line
Improved "at ... line ..." reporting for rejected async method
2 parents 03d1b64 + 05c6663 commit 4e511d0

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/Mojo/Promise.pm

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package Mojo::Promise;
22
use Mojo::Base -base;
33

4-
use Carp qw(carp);
4+
use Carp qw(carp croak);
55
use Mojo::Exception;
66
use Mojo::IOLoop;
77
use Scalar::Util qw(blessed);
@@ -18,8 +18,9 @@ sub AWAIT_FAIL { _settle_await(reject => @_) }
1818
sub AWAIT_GET {
1919
my $self = shift;
2020
my @results = @{$self->{results} // []};
21-
die $results[0] unless $self->{status} eq 'resolve';
22-
return wantarray ? @results : $results[0];
21+
return wantarray ? @results : $results[0] if $self->{status} eq 'resolve';
22+
die $results[0] if ref $results[0] || $results[0] =~ m!\n!;
23+
croak $results[0];
2324
}
2425

2526
sub AWAIT_IS_CANCELLED {undef}

t/mojo/promise_async_await.t

+6
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ subtest 'Exception handling and async/await' => sub {
137137
$t->get_ok('/four')->status_is(500)->content_like(qr/this went perfectly/);
138138
};
139139

140+
subtest 'Exception handling with file and line reporting' => sub {
141+
my $error;
142+
reject_p()->catch(sub { $error = shift })->wait;
143+
like $error, qr/^Rejected promise at .*promise_async_await\.t line \d+\.$/, 'right content';
144+
};
145+
140146
subtest 'Exception handling without "Unhandled rejected promise" warning' => sub {
141147
my ($error, @warn);
142148
local $SIG{__WARN__} = sub { push @warn, $_[0] };

0 commit comments

Comments
 (0)