Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improved render_exception and render_not_found to use the current for…
…mat if available
  • Loading branch information
kraih committed Aug 23, 2011
1 parent b2d2eba commit 5613d28
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 44 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

1.90 2011-08-23 00:00:00
1.90 2011-08-24 00:00:00
- Improved render_exception and render_not_found to use the current
format if available. (alnewkirk)
- Improved documentation.

1.89 2011-08-23 00:00:00
Expand Down
75 changes: 37 additions & 38 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -322,33 +322,21 @@ sub render_exception {
$snapshot->{$key} = $value;
}

# Mode specific template
# Render with fallbacks
my $mode = $self->app->mode;
my $options = {
template => "exception.$mode",
format => 'html',
format => $stash->{format} || 'html',
handler => undef,
status => 500,
snapshot => $snapshot,
exception => $e,
'mojo.exception' => 1
};
unless ($self->render($options)) {

# Template
$options->{template} = 'exception';
unless ($self->render($options)) {

# Inline template
delete $stash->{layout};
delete $stash->{extends};
delete $options->{template};
$options->{inline} =
$mode eq 'development' ? $DEVELOPMENT_EXCEPTION : $EXCEPTION;
$options->{handler} = 'ep';
$self->render($options);
}
}
my $inline = $mode eq 'development' ? $DEVELOPMENT_EXCEPTION : $EXCEPTION;
return if $self->_render_fallbacks($options, 'exception', $inline);
$options->{format} = 'html';
$self->_render_fallbacks($options, 'exception', $inline);
}

# DEPRECATED in Smiling Face With Sunglasses!
Expand Down Expand Up @@ -389,31 +377,19 @@ sub render_not_found {
? $self->url_for('/perldoc')
: 'http://mojolicio.us/perldoc';

# Mode specific template
# Render with fallbacks
my $mode = $self->app->mode;
my $options = {
template => "not_found.$mode",
format => 'html',
format => $stash->{format} || 'html',
status => 404,
guide => $guide,
'mojo.not_found' => 1
};
unless ($self->render($options)) {

# Template
$options->{template} = 'not_found';
unless ($self->render($options)) {

# Inline template
delete $options->{layout};
delete $options->{extends};
delete $options->{template};
$options->{inline} =
$mode eq 'development' ? $DEVELOPMENT_NOT_FOUND : $NOT_FOUND;
$options->{handler} = 'ep';
$self->render($options);
}
}
my $inline = $mode eq 'development' ? $DEVELOPMENT_NOT_FOUND : $NOT_FOUND;
return if $self->_render_fallbacks($options, 'not_found', $inline);
$options->{format} = 'html';
$self->_render_fallbacks($options, 'not_found', $inline);
}

# "You called my thesis a fat sack of barf, and then you stole it?
Expand Down Expand Up @@ -707,6 +683,29 @@ sub write_chunk {
return $self;
}

sub _render_fallbacks {
my ($self, $options, $template, $inline) = @_;

# Mode specific template
unless ($self->render($options)) {

# Template
$options->{template} = $template;
unless ($self->render($options)) {

# Inline template
my $stash = $self->stash;
return unless $stash->{format} eq 'html';
delete $stash->{layout};
delete $stash->{extends};
delete $options->{template};
$options->{inline} = $inline;
$options->{handler} = 'ep';
return $self->render($options);
}
}
}

1;
__END__
Expand Down Expand Up @@ -878,8 +877,8 @@ will not be encoded.
$c->render_exception('Oops!');
$c->render_exception(Mojo::Exception->new('Oops!'));
Render the exception template C<exception.$mode.html.$handler> or
C<exception.html.$handler> and set the response status code to C<500>.
Render the exception template C<exception.$mode.$format.$handler> or
C<exception.$format.$handler> and set the response status code to C<500>.
=head2 C<render_json>
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -612,9 +612,9 @@ to C<CPAN>.
While the built-in C<exception> and C<not_found> templates are very useful
during development, you most likely want to show your users something more
related to your application in production.
That's why L<Mojolicious> will always try to render C<exception.$mode.html.*>
or C<not_found.$mode.html.*> before falling back to the built-in default
templates.
That's why L<Mojolicious> will always try to render
C<exception.$mode.$format.*> or C<not_found.$mode.$format.*> before falling
back to the built-in default templates.

@@ exception.production.html.ep
<!doctype html><html>
Expand Down
35 changes: 33 additions & 2 deletions t/mojolicious/exception_lite_app.t
Expand Up @@ -8,7 +8,7 @@ BEGIN {
$ENV{MOJO_MODE} = 'development';
}

use Test::More tests => 32;
use Test::More tests => 54;

# "This calls for a party, baby.
# I'm ordering 100 kegs, 100 hookers and 100 Elvis impersonators that aren't
Expand Down Expand Up @@ -43,6 +43,9 @@ get '/trapped' => sub {
$self->render_text($@->{foo} || 'failed');
};

# GET /missing_template
get '/missing_template';

# Dummy exception object
package MyException;
use Mojo::Base -base;
Expand Down Expand Up @@ -82,7 +85,17 @@ $t->get_ok('/dead_template_with_layout')->status_is(500)
->content_like(qr/2\./)->content_like(qr/dead\ template\ with\ layout!/);

# GET /dead_action
$t->get_ok('/dead_action')->status_is(500)->content_like(qr/32\./)
$t->get_ok('/dead_action')->status_is(500)
->content_type_is('text/html;charset=UTF-8')->content_like(qr/32\./)
->content_like(qr/dead\ action!/);

# GET /dead_action.xml (different format)
$t->get_ok('/dead_action.xml')->status_is(500)->content_type_is('text/xml')
->content_is("<very>bad</very>\n");

# GET /dead_action.json (unsupported format)
$t->get_ok('/dead_action.json')->status_is(500)
->content_type_is('text/html;charset=UTF-8')->content_like(qr/32\./)
->content_like(qr/dead\ action!/);

# GET /double_dead_action
Expand All @@ -95,6 +108,18 @@ $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 /missing_template
$t->get_ok('/missing_template')->status_is(404)
->content_type_is('text/html;charset=UTF-8')->content_like(qr/Not Found/);

# GET /missing_template.xml (different format)
$t->get_ok('/missing_template.xml')->status_is(404)
->content_type_is('text/xml')->content_is("<somewhat>bad</somewhat>\n");

# GET /missing_template (unsupported format)
$t->get_ok('/missing_template.json')->status_is(404)
->content_type_is('text/html;charset=UTF-8')->content_like(qr/Not Found/);

__DATA__
@@ layouts/green.html.ep
%= content
Expand All @@ -110,3 +135,9 @@ works!
@@ dead_template_with_layout.html.ep
% layout 'green';
% die 'dead template with layout!';
@@ exception.xml.ep
<very>bad</very>
@@ not_found.xml.ep
<somewhat>bad</somewhat>

0 comments on commit 5613d28

Please sign in to comment.