Skip to content

Instantly share code, notes, and snippets.

@marcusramberg
Created July 25, 2012 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusramberg/3174939 to your computer and use it in GitHub Desktop.
Save marcusramberg/3174939 to your computer and use it in GitHub Desktop.
sub render_exception {
my ($self, $e) = @_;
- $e = Mojo::Exception->new($e);
- # Recursion
+ # Log exception
my $app = $self->app;
- $app->log->error($e);
- my $stash = $self->stash;
- return if $stash->{'mojo.exception'};
+ $app->log->error($e = Mojo::Exception->new($e));
# Filtered stash snapshot
+ my $stash = $self->stash;
my %snapshot = map { $_ => $stash->{$_} }
grep { !/^mojo\./ and defined $stash->{$_} } keys %$stash;
# Render with fallbacks
my $mode = $app->mode;
my $options = {
- template => "exception.$mode",
- format => $stash->{format} || 'html',
- handler => undef,
- status => 500,
- snapshot => \%snapshot,
- exception => $e,
- 'mojo.exception' => 1
+ exception => $e,
+ snapshot => \%snapshot,
+ template => "exception.$mode",
+ format => $stash->{format} || 'html',
+ handler => undef,
+ status => 500
};
my $inline = $mode eq 'development' ? $DEV_EXCEPTION : $EXCEPTION;
- return if $self->_render_fallbacks($options, 'exception', $inline);
- $options->{format} = 'html';
- $self->_render_fallbacks($options, 'exception', $inline);
+ return if $self->_fallbacks($options, 'exception', $inline);
+ $self->_fallbacks({%$options, format => 'html'}, 'exception', $inline);
}
# "If you hate intolerance and being punched in the face by me,
@@ -282,22 +278,14 @@ sub render_later { shift->stash->{'mojo.rendered'}++ }
sub render_not_found {
my $self = shift;
- # Recursion
- my $stash = $self->stash;
- return if $stash->{'mojo.exception'} || $stash->{'mojo.not_found'};
-
# Render with fallbacks
- my $mode = $self->app->mode;
- my $options = {
- template => "not_found.$mode",
- format => $stash->{format} || 'html',
- status => 404,
- 'mojo.not_found' => 1
- };
+ my $mode = $self->app->mode;
+ my $format = $self->stash->{format} || 'html';
+ my $options
+ = {template => "not_found.$mode", format => $format, status => 404};
my $inline = $mode eq 'development' ? $DEV_NOT_FOUND : $NOT_FOUND;
- return if $self->_render_fallbacks($options, 'not_found', $inline);
- $options->{format} = 'html';
- $self->_render_fallbacks($options, 'not_found', $inline);
+ return if $self->_fallbacks($options, 'not_found', $inline);
+ $self->_fallbacks({%$options, format => 'html'}, 'not_found', $inline);
}
# "You called my thesis a fat sack of barf, and then you stole it?
@@ -522,7 +510,7 @@ sub write_chunk {
return $self->rendered;
}
-sub _render_fallbacks {
+sub _fallbacks {
my ($self, $options, $template, $inline) = @_;
# Mode specific template
@@ -817,7 +805,7 @@ of the response, which is C<text/html;charset=UTF-8> by default.
$c = $c->rendered;
$c = $c->rendered(302);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment