Skip to content

Commit

Permalink
Error templates now get request path as variable
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Jul 28, 2011
1 parent 8a232b7 commit 62a3385
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
# Change file for Plack::Middleware::TemplateToolkit

0.23
Error templates now get request path as variable

0.22
Added timer

Expand Down
2 changes: 1 addition & 1 deletion dist.ini
Expand Up @@ -4,7 +4,7 @@ copyright_year = 2011
author = Leo Lapworth
author = Jakob Voss
copyright_holder = Leo Lapworth
version = 0.22
version = 0.23

[@Basic]
[InstallGuide]
Expand Down
30 changes: 15 additions & 15 deletions lib/Plack/Middleware/TemplateToolkit.pm
Expand Up @@ -111,6 +111,7 @@ sub call { # adapted from Plack::Middleware::Static

if ( !$res or ( $self->pass_through and $res->[0] == 404 ) ) {
if ( $self->app ) {
# pass to the next middleware/app
$res = $self->app->($env);
# if ( $self->catch_errors and $res->[0] =~ /^[45]/ ) {
# TODO: process error message (but better use callback)
Expand Down Expand Up @@ -166,9 +167,10 @@ sub process_error {

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

$req->env->{'tt.vars'}->{'error'} = $error;
$req->env->{'tt.vars'}->{'path'} = $req->path_info;
my $tpl = $self->{$code};
my $res = $self->process_template( $tpl, $code, $req->env->{'tt.vars'} );

Expand All @@ -195,7 +197,6 @@ sub _set_vars {

# we must not copy the vars by reference because
# otherwise we might modify the same object
# TODO: catch error if $self->vars does not return hash ref or dies?
my (%vars) = %{ $self->vars->($req) } if defined $self->vars;

my $rv = $self->request_vars;
Expand Down Expand Up @@ -268,7 +269,7 @@ sub _handle_template {

my $extension = $self->extension;
if ( $extension and $path !~ /${extension}$/ ) {
# TODO: we may want another code (forbidden) and message here
# This 404 will be catched in method call if pass_through is set
my ($res, $tpl) = $self->process_error(
404, 'Not found', 'text/plain', Plack::Request->new($env) );
$env->{'tt.template'} = $tpl;
Expand All @@ -281,7 +282,7 @@ sub _handle_template {
}

my $req = Plack::Request->new($env);
$self->_set_vars($req);
$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'} );
Expand Down Expand Up @@ -382,8 +383,9 @@ L<Plack::Builder> to map requests based on a path to this middleware.
=item extension
Limit to only files with this extension. Requests for other files will result in
a 404 response or be passed to the next application if C<pass_through> is set.
Limit to only files with this extension. Requests for other files within
C<path> will result in a 404 response or be passed to the next application if
C<pass_through> is set.
=item content_type
Expand Down Expand Up @@ -461,17 +463,15 @@ with Unicode from several sources (templates, variables, parameters, ...).
Time the processing and add C<tt.start>, C<tt.end>, and C<tt.elapsed> to the
environment.
=back
In addition you can specify templates for error codes, for instance:
=item 404 and 500
Plack::Middleware::TemplateToolkit->new(
INCLUDE_PATH => '/path/to/htdocs/',
404 => 'page_not_found.html' # = /path/to/htdocs/page_not_found.html
);
Specifies an error template that is processed when a file was not found (404)
or on server error (500). The template variables C<error> with an error message
and C<path> with the request path are set for processing. If an error template
count not be found and processed, another error with status code 500 is
returned, possibly also as template.
If a specified error templates could not be found and processed, an error
with HTTP status code 500 is returned, possibly also as template.
=back
=head1 ENVIRONMENT
Expand Down
3 changes: 2 additions & 1 deletion t/10_app.t
Expand Up @@ -36,7 +36,7 @@ app_tests
},
{ name => '404request',
request => [ GET => '/boom.html' ],
content => '404-page',
content => '404-page[% path %]', # served by ::ErrorDocument, no template
headers => { 'Content-Type' => 'text/html', },
code => 404
},
Expand Down Expand Up @@ -76,6 +76,7 @@ app_tests
},{
name => 'Unmatched request',
request => [ GET => '/style.css' ],
content => 'Not found', # default error message
code => 404,
}];

Expand Down
4 changes: 2 additions & 2 deletions t/30_errordocs.t
Expand Up @@ -61,7 +61,7 @@ app_tests
},
{ name => '404 error template',
request => [ GET => '/boom.html' ],
content => '404-page',
content => '404-page/boom.html',
headers => { 'Content-Type' => 'text/html', },
code => 404,
logged => []
Expand Down Expand Up @@ -134,7 +134,7 @@ app_tests
name => 'Unmatched request, 404 as template',
request => [ GET => '/style.css' ],
code => 404,
content => '404-page',
content => '404-page/style.css',
}];


Expand Down
2 changes: 1 addition & 1 deletion t/root/404.html
@@ -1 +1 @@
404-page
404-page[% path %]

0 comments on commit 62a3385

Please sign in to comment.