Navigation Menu

Skip to content

Commit

Permalink
added tests and recipe for custom exception rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 30, 2014
1 parent b2484c2 commit faac71b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
28 changes: 25 additions & 3 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -416,6 +416,28 @@ templates.
</body>
</html>

The hook L<Mojolicious/"before_render"> makes even more advanced
customizations possible by allowing you to intercept and modify the arguments
passed to the renderer.

use Mojolicious::Lite;

hook before_render => sub {
my ($self, $args) = @_;

# Make sure we are rendering the exception template
return unless my $template = $args->{template};
return unless $template eq 'exception';

# Switch to JSON rendering if content negotiation allows it
$args->{json} = {exception => $self->stash('exception')}
if $self->accepts('json');
};

get '/' => sub { die "This sho...ALL GLORY TO THE HYPNOTOAD!\n" };

app->start;

=head2 Helpers

Helpers are little functions you can use in templates and controller code.
Expand Down Expand Up @@ -1004,9 +1026,9 @@ that a response has been generated.

=head2 Post-processing dynamic content

While post-processing tasks are generally very easy with the C<after_dispatch>
hook, for content generated by the renderer it is a lot more efficient to use
C<after_render>.
While post-processing tasks are generally very easy with hook
L<Mojolicious/"after_dispatch">, for content generated by the renderer it is a
lot more efficient to use L<Mojolicious/"after_render">.

use Mojolicious::Lite;
use IO::Compress::Gzip 'gzip';
Expand Down
13 changes: 12 additions & 1 deletion t/mojolicious/exception_lite_app.t
Expand Up @@ -28,6 +28,13 @@ hook before_render => sub {
if ($args->{template} // '') eq 'not_found' && $args->{format} eq 'txt';
};

# Custom exception rendering for "txt"
hook before_render => sub {
my ($self, $args) = @_;
@$args{qw(text format)} = ($self->stash('exception'), 'txt')
if ($args->{template} // '') eq 'exception' && $self->accepts('txt');
};

get '/logger' => sub {
my $self = shift;
my $level = $self->param('level');
Expand All @@ -42,7 +49,7 @@ get '/dead_included_template';

get '/dead_template_with_layout';

get '/dead_action' => sub { die 'dead action!' };
get '/dead_action' => sub { die "dead action!\n" };

get '/double_dead_action_☃' => sub {
eval { die 'double dead action!' };
Expand Down Expand Up @@ -169,6 +176,10 @@ $t->get_ok('/dead_action.json')->status_is(500)
->content_like(qr!get &#39;/dead_action&#39;!)
->content_like(qr/dead action!/);

# Dead action with custom exception rendering
$t->get_ok('/dead_action' => {Accept => 'text/plain'})->status_is(500)
->content_type_is('text/plain;charset=UTF-8')->content_is("dead action!\n");

# Action dies twice
$t->get_ok('/double_dead_action_☃')->status_is(500)
->content_like(qr!get &#39;/double_dead_action_☃&#39;.*lite_app\.t:\d!s)
Expand Down

0 comments on commit faac71b

Please sign in to comment.