Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unable to use send_as during template processing (#1712) #1713

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/Dancer2/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,17 @@ sub template {
$self->set_with_return( sub {
$local_response ||= shift;
});
my $content = $template->process( @_ );
# Catch any exceptions that may happen during template processing
my $content = eval { $template->process( @_ ) };
my $eval_result = $@;
$self->set_with_return($old_with_return);
# If there was a previous response set before the exception (or set as
# part of the exception handling), then use that, otherwise throw the
# exception as normal
if ($local_response) {
$self->with_return->($local_response);
} elsif ($eval_result) {
die $eval_result;
}
return $content;
}
Expand Down Expand Up @@ -972,7 +979,9 @@ sub send_as {
$options->{charset} = $self->config->{charset} || 'UTF-8';
my $content = Encode::encode( $options->{charset}, $data );
$options->{content_type} ||= join '/', 'text', lc $type;
$self->send_file( \$content, %$options ); # returns from sub
# Explicit return needed here, as if we are currently rendering a
# template then with_return will not longjump
return $self->send_file( \$content, %$options );
}

# Try and load the serializer class
Expand Down
70 changes: 70 additions & 0 deletions t/issues/gh-1712/gh-1712.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Plack::Test;
use HTTP::Request::Common;
use Ref::Util qw<is_coderef>;

# Tests for obscure rendering situations that may occur whilst undertaking
# template processing

{
package MyApp;

use Dancer2;

set template => 'template_toolkit';

hook on_route_exception => sub {
status 200;
send_as(plain => "Some plain text");
};

get '/bork' => sub {
my $bork = sub {
die "I've borked in the template";
};
template 'exec',
{ exec_sub => $bork }
};

get '/plain' => sub {
my $plain = sub {
send_as(plain => "Some plain text");
};
template 'exec',
{ exec_sub => $plain }
};

get '/html' => sub {
my $html = sub {
send_as(html => "<p>HTML text</p>");
};
template 'exec',
{ exec_sub => $html }
};
}

my $app = Dancer2->psgi_app;
ok( is_coderef($app), 'Got app' );

test_psgi $app, sub {
my $cb = shift;

my $res = $cb->(GET '/bork');
is $res->code, 200, 'Correct status code when overriding exception handling';
like $res->content, qr/plain text/, 'Correct content when overriding exception handling';

$res = $cb->(GET '/plain');
is $res->code, 200, 'Correct status when sending as plain during template render';
like $res->content, qr/plain text/, 'Correct content when sending as plain';

$res = $cb->(GET '/html');
is $res->code, 200, 'Correct status when sending as HTML during template render';
like $res->content, qr/HTML text/, 'Correct content when sending as HTML';
};

done_testing();
1 change: 1 addition & 0 deletions t/issues/gh-1712/views/exec.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[% IF NOT exec_sub %]foobar[% END %]
Loading