[Dancer-users] Exception in before_template_render hook does not render error_template

Alex C calyx238 at gmail.com
Sat Apr 14 15:44:50 CEST 2012


After some thought, I realise that patching this is a bad idea because
then the before_template_render hook will not be called after any
exception.

Instead I will move my database call outide the hook, and I can add a
small note to the error_template documentation.

Sorry for the noise!

On 14 April 2012 06:57, Alex C <calyx238 at gmail.com> wrote:
> I noticed this when trying to make a database call in the
> before_template_render hook (to conditionally set a token if a row
> exists). To reproduce:
>
> $ echo "my custom error message" > views/error.tt
>
> ---------
> package App;
> use Dancer ':syntax';
>
> set show_errors => 0;
> set error_template => 'error.tt';
>
> hook before_template_render => sub {    die;    };
>
> get '/' => sub {    template 'index.tt';    };
>
> true;
> ---------
>
> It can be resolved by adding a simple check inside
> Dancer::Template::Abstract::apply_renderer() :
>
> ---------
> @@ -73,9 +73,13 @@ sub apply_renderer {
>
>     ($tokens, undef) = _prepare_tokens_options($tokens);
>
> +    my $is_error_template
> +        = $view eq Dancer::Config::setting('error_template') ? 1 : 0;
> +
>     $view = $self->view($view);
>
> -    Dancer::Factory::Hook->execute_hooks('before_template_render', $tokens);
> +    Dancer::Factory::Hook->execute_hooks('before_template_render', $tokens)
> +        unless $is_error_template;
> ---------
>
> Is it worth making a PR for this?
>
> --
> Alex


More information about the Dancer-users mailing list