Skip to content

Commit

Permalink
pass exception object to around_exception hook
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 5, 2012
1 parent 90a12db commit 0c0b83a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
28 changes: 18 additions & 10 deletions lib/Mojolicious.pm
Expand Up @@ -172,8 +172,8 @@ sub handler {
$c->app->dispatch($c);
}
);
$self->hook(around_exception => sub { _exception(pop) });
$self->hook(around_not_found => sub { _not_found(pop) });
$self->hook(around_exception => sub { _exception(@_) });
$self->hook(around_not_found => sub { _not_found(@_) });
$self->{started}++;
}

Expand Down Expand Up @@ -216,15 +216,22 @@ sub start { ($ENV{MOJO_APP} = shift)->commands->start(@_) }
sub startup { }

sub _exception {
my $c = shift;
my ($next, $c, $e) = @_;

# Filtered stash snapshot
my $stash = $c->stash;
my %snapshot = map { $_ => $stash->{$_} }
grep { !/^mojo\./ and defined $stash->{$_} } keys %$stash;

# Render with fallbacks
my $mode = $c->app->mode;
my $options = {
template => "exception.$mode",
format => $c->stash->{format} || 'html',
handler => undef,
status => 500
exception => $e,
snapshot => \%snapshot,
template => "exception.$mode",
format => $stash->{format} || 'html',
handler => undef,
status => 500
};
my $inline = $mode eq 'development' ? $DEV_EXCEPTION : $EXCEPTION;
return if _fallbacks($c, $options, 'exception', $inline);
Expand Down Expand Up @@ -253,7 +260,7 @@ sub _fallbacks {
}

sub _not_found {
my $c = shift;
my ($next, $c) = @_;

# Render with fallbacks
my $mode = $c->app->mode;
Expand Down Expand Up @@ -576,14 +583,15 @@ in the chain will use the renderer to try and find an exception template or
fall back to a built-in one.
$app->hook(around_exception => sub {
my ($next, $c) = @_;
my ($next, $c, $e) = @_;
...
$next->();
...
});
Useful for monitoring and generating custom exception responses. (Passed a
closure leading to the next hook and the current controller object)
closure leading to the next hook, the current controller object and an
exception object)
=item C<around_not_found>
Expand Down
12 changes: 1 addition & 11 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -232,19 +232,9 @@ sub render_data { shift->render(data => @_) }
# Neat."
sub render_exception {
my ($self, $e) = @_;

# Log exception
my $app = $self->app;
$app->log->error($e = Mojo::Exception->new($e));

# Filtered stash snapshot
my $stash = $self->stash;
my %snapshot = map { $_ => $stash->{$_} }
grep { !/^mojo\./ and defined $stash->{$_} } keys %$stash;

# Delegate
%$stash = (%$stash, snapshot => \%snapshot, exception => $e);
$app->plugins->emit_chain(around_exception => $self);
$app->plugins->emit_chain('around_exception', $self, $e);
}

# "If you hate intolerance and being punched in the face by me,
Expand Down
6 changes: 3 additions & 3 deletions t/mojolicious/exception_lite_app.t
Expand Up @@ -21,8 +21,8 @@ app->renderer->paths->[0] = app->home->rel_dir('does_not_exist');

# Custom exception handling
hook around_exception => sub {
my ($next, $self) = @_;
return $self->render(inline => 'Custom exception: <%= $exception %>')
my ($next, $self, $e) = @_;
return $self->render(text => "Custom exception: $e")
if $self->req->url->path->contains('/custom/exception');
$next->();
};
Expand Down Expand Up @@ -199,7 +199,7 @@ $t->get_ok('/custom')->status_is(200)->content_is('Custom handling works!');

# GET /custom/exception
$t->get_ok('/custom/exception')->status_is(200)
->content_is("Custom exception: Custom dead!\n\n");
->content_is("Custom exception: Custom dead!\n");
like $log, qr/Custom dead!/, 'right result';

# GET /custom/not_found
Expand Down

0 comments on commit 0c0b83a

Please sign in to comment.