Skip to content

Commit

Permalink
simplified exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 12, 2009
1 parent c6dc703 commit e6b19ab
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This file documents the revision history for Perl extension Mojo.
is not backward compatible and you might have to change some of
your tests.
- Simplified MojoX::Renderer.
- Simplified exceptions.
- Updated exception handling in Mojolicious to work with exceptions
in epl templates.
- Added html_encode and html_decode methods to Mojo::ByteStream.
Expand Down
2 changes: 0 additions & 2 deletions lib/Mojo/Template.pm
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,11 @@ But L<Mojo::Template> will return L<Mojo::Template::Exception> objects that
stringify to error messages with context.
Error around line 4.
-----------------------------------------------------------------
2: </head>
3: <body>
4: % my $i = 2; xx
5: %= $i * 2
6: </body>
-----------------------------------------------------------------
Bareword "xx" not allowed while "strict subs" in use at (eval 13)
line 4.
Expand Down
25 changes: 19 additions & 6 deletions lib/Mojo/Template/Exception.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use warnings;
use base 'Mojo::Base';
use overload '""' => sub { shift->to_string }, fallback => 1;

__PACKAGE__->attr([qw/line lines_before lines_after/], default => sub { [] });
__PACKAGE__->attr([qw/line lines_before lines_after stack/],
default => sub { [] });
__PACKAGE__->attr('message', default => 'Exception!');

# Attempted murder? Now honestly, what is that?
Expand All @@ -33,12 +34,14 @@ sub new {
my $i = 1;
while (my ($p, $f, $l) = caller($i++)) {

# Stack
push @{$self->stack}, [$f, $l];

# Found?
if ($p eq $caller && $f =~ /^\(eval\s+\d+\)$/) {

# Done
$line = $l;
last;
}
}

Expand Down Expand Up @@ -103,8 +106,7 @@ sub to_string {
my $string = '';

# Header
my $delim = '-' x 76;
$string .= ('Error around line ' . $self->line->[0] . ".\n$delim\n")
$string .= ('Error around line ' . $self->line->[0] . ".\n")
if $self->line->[0];

# Before
Expand All @@ -121,8 +123,14 @@ sub to_string {
$string .= $line->[0] . ': ' . $line->[1] . "\n";
}

# Delim
$string .= "$delim\n" if length $string;
# Stack
if (@{$self->stack}) {
for my $frame (@{$self->stack}) {
my $file = $frame->[0];
my $line = $frame->[1];
$string .= "$file: $line\n";
}
}

# Message
$string .= $self->message if $self->message;
Expand Down Expand Up @@ -170,6 +178,11 @@ L<Mojo::Template::Exception> implements the following attributes.
my $message = $e->message;
$e = $e->message('oops!');
=head2 C<stack>
my $stack = $e->line;
$e = $e->line([['/foo/bar.pl', 23], ['/bar.pl', 2]]);
=head1 METHODS
L<Mojo::Template::Exception> inherits all methods from L<Mojo::Base> and
Expand Down
2 changes: 0 additions & 2 deletions t/mojo/loader.t
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ is($e->line->[1], "1;");
$e->message("oops!\n");
is("$e", <<'EOF');
Error around line 15.
----------------------------------------------------------------------------
13: foo {
14:
15: 1;
----------------------------------------------------------------------------
oops!
EOF

Expand Down
11 changes: 5 additions & 6 deletions t/mojo/template.t
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ is($output->lines_before->[1]->[1], '%= 1 + 1');
is($output->line->[0], 5);
is($output->line->[1], 'test');
$output->message("oops!\n");
$output->stack([['foo', 23], ['bar', 24]]);
is("$output", <<'EOF');
Error around line 5.
----------------------------------------------------------------------------
3: % {
4: %= 1 + 1
5: test
----------------------------------------------------------------------------
foo: 23
bar: 24
oops!
EOF

Expand All @@ -112,15 +113,14 @@ is($output->lines_after->[0]->[1], '%= 1 + 1');
is($output->lines_after->[1]->[0], 5);
is($output->lines_after->[1]->[1], 'test');
$output->message("oops!\n");
$output->stack([]);
is("$output", <<'EOF');
Error around line 3.
----------------------------------------------------------------------------
1: test
2: 123
3: %= MyTemplateException->exception
4: %= 1 + 1
5: test
----------------------------------------------------------------------------
oops!
EOF

Expand All @@ -147,15 +147,14 @@ is($output->lines_after->[0]->[1], '%= 1 + 1');
is($output->lines_after->[1]->[0], 5);
is($output->lines_after->[1]->[1], 'test');
$output->message("oops!\n");
$output->stack([]);
is("$output", <<'EOF');
Error around line 3.
----------------------------------------------------------------------------
1: test
2: 123
3: % die 'oops!';
4: %= 1 + 1
5: test
----------------------------------------------------------------------------
oops!
EOF

Expand Down

0 comments on commit e6b19ab

Please sign in to comment.