Navigation Menu

Skip to content

Commit

Permalink
made Mojo::Exception quite a bit smarter
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 11, 2010
1 parent 3cd4465 commit 3f0b598
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 29 deletions.
83 changes: 55 additions & 28 deletions lib/Mojo/Exception.pm
Expand Up @@ -19,6 +19,44 @@ __PACKAGE__->attr(verbose => sub { $ENV{MOJO_EXCEPTION_VERBOSE} || 0 });
sub new {
my $self = shift->SUPER::new();

# Message
return $self unless @_;

# Detect
return $self->_detect(@_);
}

sub throw {
my $self = shift;

# Trace
my @trace;
my $i = 1;
while (my ($p, $f, $l) = caller($i++)) {

# Append
push @trace, [$p, $f, $l];

# Line
if (-r $f) {
next unless my $handle = IO::File->new("< $f");
my @lines = <$handle>;
push @{$trace[-1]}, $lines[$l - 1];
}
}

# Exception
my $e = Mojo::Exception->new;
$e->trace(\@trace);
$e->_detect(@_);

# Throw
die $e;
}

sub _detect {
my $self = shift;

# Message
my $message = shift;
return $message if blessed $message && $message->isa('Mojo::Exception');
Expand All @@ -31,6 +69,12 @@ sub new {
push @trace, {file => $1, line => $2};
}

# Stacktrace
if (my $first = $self->trace->[0]) {
unshift @trace, {file => $first->[1], line => $first->[2]}
if $first->[1];
}

# Frames
foreach my $frame (reverse @trace) {

Expand All @@ -49,7 +93,7 @@ sub new {
$self->_parse_context($line, [\@lines]);

# Done
last;
return $self;
}
}

Expand All @@ -76,37 +120,20 @@ sub new {
my $line;
$line = $1 if $self->message =~ /at\s+template\s+line\s+(\d+)/;

# Context
$self->_parse_context($line, \@lines) if $line;

return $self;
}

sub throw {
my $self = shift;

# Trace
my @trace;
my $i = 1;
while (my ($p, $f, $l) = caller($i++)) {

# Append
push @trace, [$p, $f, $l];

# Line
if (-r $f) {
next unless my $handle = IO::File->new("< $f");
my @lines = <$handle>;
push @{$trace[-1]}, $lines[$l - 1];
# Stacktrace
unless ($line) {
for my $frame (@{$self->trace}) {
if ($frame->[1] =~ /^\(eval\ \d+\)$/) {
$line = $frame->[2];
last;
}
}
}

# Exception
my $e = Mojo::Exception->new(@_);
$e->trace(\@trace);
# Context
$self->_parse_context($line, \@lines) if $line;

# Throw
die $e;
return $self;
}

# You killed zombie Flanders!
Expand Down
20 changes: 19 additions & 1 deletion t/mojo/template.t
Expand Up @@ -23,7 +23,7 @@ package main;
use strict;
use warnings;

use Test::More tests => 124;
use Test::More tests => 133;

use File::Spec;
use File::Temp;
Expand Down Expand Up @@ -506,6 +506,24 @@ $output = $mt->render(<<'EOF');
EOF
is $output, "Mojo::Template\nworks!\n", 'right result';

# Unusable error message (stacktrace required)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
test
123
% die "x\n";
test
EOF
is ref $output, 'Mojo::Exception', 'right exception';
is $output->message, "x\n", 'right message';
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';
is $output->line->[1], '% die "x\n";', 'right line';
like "$output", qr/^x/, 'right result';

# Compile time exception
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
Expand Down

0 comments on commit 3f0b598

Please sign in to comment.