Skip to content

Commit

Permalink
Merge pull request #1948 from andrew-grechkin/issue-1947
Browse files Browse the repository at this point in the history
traceback in Mojo::Exception print fully qualified sub names
  • Loading branch information
mergify[bot] authored Aug 20, 2022
2 parents 2517d80 + 0b5abea commit 157fce2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/Exception.pm
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ sub to_string {

if (my $max = @$frames) {
$str .= "Traceback (most recent call first):\n";
$str .= qq{ File "$_->[1]", line $_->[2], in "$_->[0]"\n} for @$frames;
$str .= qq{ File "$_->[1]", line $_->[2], in "$_->[3]"\n} for @$frames;
}

return $str;
Expand Down
40 changes: 33 additions & 7 deletions t/mojo/exception.t
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ subtest 'Context' => sub {
$e = Mojo::Exception->new->inspect->inspect;
is $e, "Exception!\n", 'right result';
$e = Mojo::Exception->new('Test!');
$e->frames([['Sandbox', 'template', 4], ['MyApp::Test', 'MyApp/Test.pm', 3], ['main', 'foo.pl', 4]]);
$e->frames(
[
['Sandbox', 'template', 4, 'Sandbox::sub'],
['MyApp::Test', 'MyApp/Test.pm', 3, 'MyApp::Test::sub'],
['main', 'foo.pl', 4, 'main::sub'],
],
);
$e->lines_before([[3, 'foo();']])->line([4, 'die;'])->lines_after([[5, 'bar();']]);
is $e, "Test! at template line 4.\n", 'right result';
is $e->verbose(1), <<EOF, 'right result';
Expand All @@ -126,19 +132,39 @@ Context:
4: die;
5: bar();
Traceback (most recent call first):
File "template", line 4, in "Sandbox"
File "MyApp/Test.pm", line 3, in "MyApp::Test"
File "foo.pl", line 4, in "main"
File "template", line 4, in "Sandbox::sub"
File "MyApp/Test.pm", line 3, in "MyApp::Test::sub"
File "foo.pl", line 4, in "main::sub"
EOF
$e->message("Works!\n")->lines_before([])->lines_after([]);
is $e, <<EOF, 'right result';
Works!
Context:
4: die;
Traceback (most recent call first):
File "template", line 4, in "Sandbox"
File "MyApp/Test.pm", line 3, in "MyApp::Test"
File "foo.pl", line 4, in "main"
File "template", line 4, in "Sandbox::sub"
File "MyApp/Test.pm", line 3, in "MyApp::Test::sub"
File "foo.pl", line 4, in "main::sub"
EOF
};

subtest 'Traceback (real frames)' => sub {
sub mock_frames { frame_level2(@_) }

sub frame_level2 {
my ($e) = @_;
$e->trace;
@{$e->frames} = grep { $_->[1] =~ m/^t/ } @{$e->frames};
}

my $e = Mojo::Exception->new;
my @frames = mock_frames($e);
my $expected_backtrace = join "\n", map {qq( File "$_->[1]", line $_->[2], in "$_->[3]")} @frames;

is $e->verbose(1), <<"EOF", 'right result';
Exception! at $frames[0][1] line $frames[0][2].
Traceback (most recent call first):
$expected_backtrace
EOF
};

Expand Down
6 changes: 3 additions & 3 deletions t/mojo/template.t
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ is $output->lines_after->[0][0], 6, 'right number';
is $output->lines_after->[0][1], '%= 1 + 1', 'right line';
is $output->lines_after->[1][0], 7, 'right number';
is $output->lines_after->[1][1], 'test', 'right line';
$output->frames([['Sandbox', 'template', 5], ['main', 'template.t', 673]]);
$output->frames([['Sandbox', 'template', 5, 'Sandbox::sub'], ['main', 'template.t', 673, 'main::sub']]);
is $output, <<EOF, 'right result';
oops! at template line 5.
Context:
Expand All @@ -720,8 +720,8 @@ Context:
6: %= 1 + 1
7: test
Traceback (most recent call first):
File "template", line 5, in "Sandbox"
File "template.t", line 673, in "main"
File "template", line 5, in "Sandbox::sub"
File "template.t", line 673, in "main::sub"
EOF

# Exception in template (empty perl lines)
Expand Down

0 comments on commit 157fce2

Please sign in to comment.