Skip to content

Commit

Permalink
handle custom exceptions gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 26, 2016
1 parent f43f5fd commit aa8f6cf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -6,6 +6,7 @@ use Mojo::Collection;
use Mojo::Exception;
use Mojo::IOLoop;
use Mojo::Util qw(dumper hmac_sha1_sum steady_time);
use Scalar::Util 'blessed';

sub register {
my ($self, $app) = @_;
Expand Down Expand Up @@ -87,7 +88,7 @@ sub _development {
my ($page, $c, $e) = @_;

my $app = $c->app;
$app->log->error($e = ref $e ? $e : Mojo::Exception->new($e)->inspect)
$app->log->error($e = _exception($e) ? $e : Mojo::Exception->new($e)->inspect)
if $page eq 'exception';

# Filtered stash snapshot
Expand All @@ -112,6 +113,8 @@ sub _development {
return $c;
}

sub _exception { blessed $_[0] && $_[0]->isa('Mojo::Exception') }

sub _fallbacks {
my ($c, $options, $template, $bundled) = @_;

Expand Down
5 changes: 5 additions & 0 deletions t/mojolicious/exception_lite_app.t
Expand Up @@ -43,6 +43,8 @@ get '/logger' => sub {
$c->render(text => "$level: $msg");
};

get '/custom_exception' => sub { die Mojo::Base->new };

get '/dead_template';

get '/dead_included_template';
Expand Down Expand Up @@ -145,6 +147,9 @@ $t->get_ok('/does_not_exist')->status_is(404)->element_exists('#mojobar')
$t->post_ok('/does_not_exist')->status_is(404)
->content_like(qr!/does_not_exist!);

# Custom exception
$t->get_ok('/custom_exception')->status_is(500)->content_like(qr/Mojo::Base/);

# Dead template
$t->get_ok('/dead_template')->status_is(500)->content_like(qr/dead template!/)
->content_like(qr/line 1/);
Expand Down

0 comments on commit aa8f6cf

Please sign in to comment.