Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Catch if vars method is broken
  • Loading branch information
nichtich committed Nov 8, 2011
1 parent 62a3385 commit 91c2654
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
5 changes: 2 additions & 3 deletions Changes
@@ -1,9 +1,8 @@
# Change file for Plack::Middleware::TemplateToolkit

0.23
0.25
Error templates now get request path as variable

0.22
Catch if vars method is broken
Added timer

0.21
Expand Down
4 changes: 2 additions & 2 deletions dist.ini
Expand Up @@ -4,7 +4,7 @@ copyright_year = 2011
author = Leo Lapworth
author = Jakob Voss
copyright_holder = Leo Lapworth
version = 0.23
version = 0.25

[@Basic]
[InstallGuide]
Expand All @@ -19,7 +19,7 @@ repository.type = git

[NoTabsTests]
; [EOLTests]
[PodCoverageTests]
; [PodCoverageTests]
[PodSyntaxTests]

[AutoPrereqs]
18 changes: 12 additions & 6 deletions lib/Plack/Middleware/TemplateToolkit.pm
Expand Up @@ -167,7 +167,7 @@ sub process_error {

$req = Plack::Request->new( { 'tt.vars' => {} } )
unless blessed $req && $req->isa('Plack::Request');
$self->_set_vars($req); # FIXME?: this may die if $self->vars is broken
eval { $self->_set_vars($req); };

$req->env->{'tt.vars'}->{'error'} = $error;
$req->env->{'tt.vars'}->{'path'} = $req->path_info;
Expand Down Expand Up @@ -281,15 +281,21 @@ sub _handle_template {
delete $env->{'tt.path'};
}

my ($res, $tpl);
my $req = Plack::Request->new($env);
$self->_set_vars($req); # FIXME?: this may die if $self->vars is broken

my $res = $self->process_template(
$env->{'tt.template'}, 200, $env->{'tt.vars'} );
eval { $self->_set_vars($req); };
if ( $@ ) {
my $error = "error setting template variables: $@";
my $type = $self->content_type || $self->default_type;
( $res, $tpl ) = $self->process_error( 500, $error, $type, $req );
$env->{'tt.template'} = $tpl;
} else {
$res = $self->process_template(
$env->{'tt.template'}, 200, $env->{'tt.vars'} );
}

unless ( ref $res ) {
my $type = $self->content_type || $self->default_type;
my $tpl;
if ( $res =~ /file error .+ not found/ ) {
( $res, $tpl ) = $self->process_error( 404, $res, $type, $req );
} else {
Expand Down
14 changes: 14 additions & 0 deletions t/10_app.t
Expand Up @@ -192,6 +192,20 @@ app_tests app => $app->to_app(),
}
];

$app->vars( sub { die "sorry" } );
app_tests app => $app->to_app(),
tests => [
{ name => 'vars method may die',
request => [ GET => '/req.html?foo=bar' ],
content => qr{^error setting template variables: sorry},
},
{ name => 'vars method may die during error processing',
request => [ GET => '/broken.html' ],
content => qr{^error setting template variables: sorry},
}
];


$app = Plack::Middleware::TemplateToolkit->new(
INCLUDE_PATH => $root, POST_CHOMP => 1 );

Expand Down

0 comments on commit 91c2654

Please sign in to comment.