Skip to content

Commit

Permalink
Merge pull request #2065 from rawleyfowler/stash-exceptions
Browse files Browse the repository at this point in the history
Add exceptions to stash regardless of format
  • Loading branch information
mergify[bot] committed May 8, 2023
2 parents 94c79cd + c9ffe4e commit 20b20d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -90,6 +90,11 @@ sub _content {
return Mojo::ByteStream->new($hash->{$name} // '');
}

sub _convert_to_exception {
my $e = shift;
return (blessed $e && $e->isa('Mojo::Exception')) ? $e : Mojo::Exception->new($e);
}

sub _csrf_token { $_[0]->session->{csrf_token} ||= hmac_sha1_sum($$ . steady_time . rand, $_[0]->app->secrets->[0]) }

sub _current_route {
Expand All @@ -100,7 +105,7 @@ sub _current_route {
sub _development {
my ($page, $c, $e) = @_;

$c->helpers->log->error(($e = _is_e($e) ? $e : Mojo::Exception->new($e))->inspect) if $page eq 'exception';
$c->helpers->log->error(($e = _convert_to_exception($e))->inspect) if $page eq 'exception';

# Filtered stash snapshot
my $stash = $c->stash;
Expand Down Expand Up @@ -187,15 +192,14 @@ sub _inactivity_timeout {
return $c;
}

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

sub _is_fresh {
my ($c, %options) = @_;
return $c->app->static->is_fresh($c, \%options);
}

sub _json_exception {
my ($c, $e) = @_;
$c->stash->{exception} = _convert_to_exception($e);
return $c->render(json => {error => $e}, status => 500) if $c->app->mode eq 'development';
return $c->render(json => {error => 'Internal Server Error'}, status => 500);
}
Expand Down Expand Up @@ -315,6 +319,7 @@ sub _tx_error { (shift->error // {})->{message} // 'Unknown error' }

sub _txt_exception {
my ($c, $e) = @_;
$c->stash->{exception} = _convert_to_exception($e);
return $c->render(text => $e, format => 'txt', status => 500) if $c->app->mode eq 'development';
return $c->render(text => 'Internal Server Error', format => 'txt', status => 500);
}
Expand Down
10 changes: 8 additions & 2 deletions t/mojolicious/exception_lite_app.t
Expand Up @@ -302,9 +302,12 @@ subtest 'JSON exceptions' => sub {
->json_like('/error', qr/dead template!/);
$t->get_ok('/does_not_exist')->status_is(404)->content_type_is('application/json;charset=UTF-8')
->json_is({error => 'Not Found'});

my $stash;
$t->app->hook(after_dispatch => sub { $stash = shift->stash });
$t->get_ok('/txt/exception')->status_is(500)->header_is('X-Text' => 'txt')
->content_type_is('text/plain;charset=UTF-8')->content_like(qr/Text exception/);
ok $stash->{exception}, 'exception exists in stash';
isa_ok $stash->{exception}, 'Mojo::Exception', 'is stash exception correct type?';

$t->app->mode('production');
$t->get_ok('/dead_template')->status_is(500)->content_type_is('application/json;charset=UTF-8')
Expand All @@ -319,9 +322,12 @@ subtest 'Text exceptions' => sub {
$t->get_ok('/dead_template')->status_is(500)->content_type_is('text/plain;charset=UTF-8')
->content_like(qr/dead template!/);
$t->get_ok('/does_not_exist')->status_is(404)->content_type_is('text/plain;charset=UTF-8')->content_is('Not Found');

my $stash;
$t->app->hook(after_dispatch => sub { $stash = shift->stash });
$t->get_ok('/txt/exception')->status_is(500)->header_is('X-Text' => 'txt')
->content_type_is('text/plain;charset=UTF-8')->content_like(qr/Text exception/);
ok $stash->{exception}, 'exception exists in stash';
isa_ok $stash->{exception}, 'Mojo::Exception', 'is stash exception correct type?';

$t->app->mode('production');
$t->get_ok('/dead_template')->status_is(500)->content_type_is('text/plain;charset=UTF-8')
Expand Down

0 comments on commit 20b20d6

Please sign in to comment.