Skip to content

Commit

Permalink
test custom exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 14, 2012
1 parent 76e71e2 commit c340713
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
17 changes: 9 additions & 8 deletions lib/Mojolicious.pm
Expand Up @@ -91,13 +91,6 @@ sub new {
$self->plugin('RequestTimer');
$self->plugin('PoweredBy');

# Reduced log output outside of development mode
$self->log->level('info') unless $mode eq 'development';

# Run mode
$mode = $mode . '_mode';
$self->$mode(@_) if $self->can($mode);

# Exception handling
$self->hook(
around_dispatch => sub {
Expand All @@ -108,6 +101,13 @@ sub new {
}
);

# Reduced log output outside of development mode
$self->log->level('info') unless $mode eq 'development';

# Run mode
$mode = $mode . '_mode';
$self->$mode(@_) if $self->can($mode);

# Startup
$self->startup(@_);

Expand Down Expand Up @@ -521,7 +521,8 @@ want to continue the chain.
...
});
This is a very powerful hook and should not be used lightly, consider it the
This is a very powerful hook and should not be used lightly, it allows you to
customize application wide exception handling for example, consider it the
sledgehammer in your toolbox. (Passed a closure leading to the next hook and
the current controller object)
Expand Down
17 changes: 16 additions & 1 deletion t/mojolicious/exception_lite_app.t
Expand Up @@ -9,7 +9,7 @@ BEGIN {
$ENV{MOJO_MODE} = 'development';
}

use Test::More tests => 80;
use Test::More tests => 83;

# "This calls for a party, baby.
# I'm ordering 100 kegs, 100 hookers and 100 Elvis impersonators that aren't
Expand Down Expand Up @@ -86,9 +86,21 @@ hook after_dispatch => sub {
$exception = $self->stash('exception');
};

# Custom exception handling
hook around_dispatch => sub {
my ($next, $self) = @_;
unless (eval { $next->(); 1 }) {
die $@ unless $@ eq "CUSTOM\n";
$self->render(text => 'Custom handling works!');
}
};

# GET /reuse/exception
get '/reuse/exception' => sub { die "Reusable exception.\n" };

# GET /custom
get '/custom' => sub { die "CUSTOM\n" };

my $t = Test::Mojo->new;

# GET /logger (debug)
Expand Down Expand Up @@ -163,6 +175,9 @@ $t->get_ok('/trapped')->status_is(200)->content_is('bar');
# GET /trapped/too
$t->get_ok('/trapped/too')->status_is(200)->content_is('works');

# GET /custom
$t->get_ok('/custom')->status_is(200)->content_is('Custom handling works!');

# GET /missing_template
$t->get_ok('/missing_template')->status_is(404)
->content_type_is('text/html;charset=UTF-8')
Expand Down

0 comments on commit c340713

Please sign in to comment.