Skip to content

Commit

Permalink
add filename attribute to Mojo::Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 26, 2016
1 parent 1c9f97e commit 2b4a100
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

6.49 2016-02-26
- Added filename attribute to Mojo::Exception.
- Added inspect method to Mojo::Exception.
- Improved check_box and radio_button helpers with support for "on" default
values.
Expand Down
34 changes: 21 additions & 13 deletions lib/Mojo/Exception.pm
Expand Up @@ -2,29 +2,30 @@ package Mojo::Exception;
use Mojo::Base -base;
use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;

has [qw(filename verbose)];
has [qw(frames line lines_before lines_after)] => sub { [] };
has message => 'Exception!';
has 'verbose';

sub inspect {
my ($self, @files) = @_;
my ($self, @source) = @_;

# Extract file and line from message
my @trace;
my $msg = $self->line([])->lines_before([])->lines_after([])->message;
while ($msg =~ /at\s+(.+?)\s+line\s+(\d+)/g) { unshift @trace, [$1, $2] }

# Extract file and line from stack trace
my $first = $self->frames->[0];
push @trace, [$first->[1], $first->[2]] if $first;
if (my $zero = $self->frames->[0]) { push @trace, [$zero->[1], $zero->[2]] }

# Search for context in files
for my $frame (@trace) {
next unless -r $frame->[0] && open my $handle, '<:utf8', $frame->[0];
$self->_context($frame->[1], [[<$handle>]]);
$self->filename($frame->[0])->_context($frame->[1], [[<$handle>]]);
return $self;
}
$self->_context($trace[-1][1], [map { [split "\n"] } @files]) if @files;

# Search for context in source
$self->_context($trace[-1][1], [map { [split "\n"] } @source]) if @source;

return $self;
}
Expand Down Expand Up @@ -114,6 +115,13 @@ L<Mojo::Exception> is a container for exceptions with context information.
L<Mojo::Exception> implements the following attributes.
=head2 filename
my $name = $e->filename;
$e = $e->filename('test.pl');
The file where the exception occurred if available.
=head2 frames
my $frames = $e->frames;
Expand All @@ -128,21 +136,21 @@ Stack trace if available.
=head2 line
my $line = $e->line;
$e = $e->line([3 => 'die;']);
$e = $e->line([3, 'die;']);
The line where the exception occurred if available.
=head2 lines_after
my $lines = $e->lines_after;
$e = $e->lines_after([[4 => 'say $foo;'], [5 => 'say $bar;']]);
$e = $e->lines_after([[4, 'say $foo;'], [5, 'say $bar;']]);
Lines after the line where the exception occurred if available.
=head2 lines_before
my $lines = $e->lines_before;
$e = $e->lines_before([[1 => 'my $foo = 8;'], [2 => 'my $bar = 9;']]);
$e = $e->lines_before([[1, 'my $foo = 23;'], [2, 'my $bar = 24;']]);
Lines before the line where the exception occurred if available.
Expand All @@ -168,10 +176,10 @@ following new ones.
=head2 inspect
$e = $e->inspect;
$e = $e->inspect($file1, $file2);
$e = $e->inspect($source1, $source2);
Inspect L</"message"> and L</"frames"> to fill L</"lines_before">, L</"line">
and L</"lines_after"> with context information.
Inspect L</"message"> and L</"frames"> to fill L</"filename">,
L</"lines_before">, L</"line"> and L</"lines_after"> with context information.
=head2 new
Expand All @@ -183,7 +191,7 @@ Construct a new L<Mojo::Exception> object and assign L</"message"> if necessary.
=head2 throw
Mojo::Exception->throw('Something went wrong!');
Mojo::Exception->throw('Something went wrong!', $file1, $file2);
Mojo::Exception->throw('Something went wrong!', $source1, $source2);
Throw exception from the current execution context.
Expand Down
1 change: 1 addition & 0 deletions t/mojo/exception.t
Expand Up @@ -25,6 +25,7 @@ eval {
$e = $@;
isa_ok $e, 'Mojo::Exception', 'right class';
is $e, 'Works!', 'right result';
like $e->filename, qr/exception\.t/, 'right name';
like $e->frames->[0][1], qr/exception\.t/, 'right file';
is $e->lines_before->[0][0], 15, 'right number';
is $e->lines_before->[0][1], 'eval {', 'right line';
Expand Down
8 changes: 6 additions & 2 deletions t/mojo/template.t
Expand Up @@ -609,8 +609,10 @@ is $output->lines_before->[0][0], 1, 'right number';
is $output->lines_before->[0][1], 'test', 'right line';
is $output->lines_before->[1][0], 2, 'right number';
is $output->lines_before->[1][1], '123', 'right line';
is $output->line->[0], 3, 'right number';
ok $output->lines_before->[1][2], 'contains code';
is $output->line->[0], 3, 'right number';
is $output->line->[1], '% die "x\n";', 'right line';
ok $output->line->[2], 'contains code';
like "$output", qr/^x/, 'right result';

# Compile time exception
Expand Down Expand Up @@ -1089,7 +1091,8 @@ $mt = Mojo::Template->new;
$file = catfile dirname(__FILE__), 'templates', 'exception.mt';
$output = $mt->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/exception\.mt line 2/, 'message contains filename';
like $output->message, qr/exception\.mt line 2/, 'message contains filename';
like $output->filename, qr/exception\.mt/, 'right name';
is $output->lines_before->[0][0], 1, 'right number';
is $output->lines_before->[0][1], 'test', 'right line';
is $output->line->[0], 2, 'right number';
Expand All @@ -1104,6 +1107,7 @@ $output = $mt->name('"foo.mt" from DATA section')->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/foo\.mt from DATA section line 2/,
'message contains filename';
is $output->filename, undef, 'no name';
is $output->lines_before->[0][0], 1, 'right number';
is $output->lines_before->[0][1], 'test', 'right line';
is $output->line->[0], 2, 'right number';
Expand Down

0 comments on commit 2b4a100

Please sign in to comment.