-
-
Save anonymous/3c4ffb1d725a411b16db to your computer and use it in GitHub Desktop.
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
diff --git a/lib/Mojo/Exception.pm b/lib/Mojo/Exception.pm | |
index 5a385f6..b8f07ae 100644 | |
--- a/lib/Mojo/Exception.pm | |
+++ b/lib/Mojo/Exception.pm | |
@@ -10,8 +10,9 @@ sub inspect { | |
my ($self, @sources) = @_; | |
# Extract file and line from message | |
+ return $self if @{$self->line}; | |
my @trace; | |
- my $msg = $self->lines_before([])->line([])->lines_after([])->message; | |
+ my $msg = $self->message; | |
while ($msg =~ /at\s+(.+?)\s+line\s+(\d+)/g) { unshift @trace, [$1, $2] } | |
# Extract file and line from stack trace | |
@@ -50,6 +51,7 @@ sub throw { CORE::die shift->new(shift)->trace(2)->inspect } | |
sub trace { | |
my ($self, $start) = (shift, shift // 1); | |
+ return $self if @{$self->frames}; | |
my @frames; | |
while (my @trace = caller($start++)) { push @frames, \@trace } | |
return $self->frames(\@frames); | |
diff --git a/t/mojo/exception.t b/t/mojo/exception.t | |
index ed0f3f5..f82c314 100644 | |
--- a/t/mojo/exception.t | |
+++ b/t/mojo/exception.t | |
@@ -57,6 +57,7 @@ like wrapper1()->frames->[0][3], qr/wrapper2/, 'right subroutine'; | |
like wrapper1(0)->frames->[0][3], qr/trace/, 'right subroutine'; | |
like wrapper1(1)->frames->[0][3], qr/wrapper2/, 'right subroutine'; | |
like wrapper1(2)->frames->[0][3], qr/wrapper1/, 'right subroutine'; | |
+like wrapper1()->trace->frames->[0][3], qr/wrapper2/, 'right subroutine'; | |
# Inspect | |
$e = Mojo::Exception->new("Whatever at @{[__FILE__]} line 3."); | |
@@ -68,6 +69,10 @@ is_deeply $e->lines_before->[-1], [2, ''], 'right line'; | |
is_deeply $e->line, [3, 'use Test::More;'], 'right line'; | |
is_deeply $e->lines_after->[0], [4, 'use Mojo::Exception;'], 'right line'; | |
$e->message("Died at @{[__FILE__]} line 4.")->inspect; | |
+is_deeply $e->lines_before->[-1], [2, ''], 'right line'; | |
+is_deeply $e->line, [3, 'use Test::More;'], 'right line'; | |
+is_deeply $e->lines_after->[0], [4, 'use Mojo::Exception;'], 'right line'; | |
+$e = $e->new("Died at @{[__FILE__]} line 4.")->inspect; | |
is_deeply $e->lines_before->[-1], [3, 'use Test::More;'], 'right line'; | |
is_deeply $e->line, [4, 'use Mojo::Exception;'], 'right line'; | |
is_deeply $e->lines_after->[0], [5, ''], 'right line'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment