> I'd like to specify a template to catch exceptions thrown by the
> Template Toolkit itself (f.e. syntax errors in my directives) in
> addition to those exceptions I throw with a THROW directive. I am
> currently catching these exceptions like so:
>
> $::template->process("MyTemplate.atml", $::vars)
> || MyExceptionHandlingFunction($::template->error());
>
> Is there any way to specify that these types of errors should be handled
> by the template specified in the ERROR configuration option (perhaps
> with a fall-back to an exception handling mechanism if that template
> also fails)?
In general, TT exceptions do go through the same mechanism as THROW.
However, syntax errors in the top-level template do not. It looks
like you have found a bug. Here are two solutions:
1) As a work-around, manually call the error template yourself:
$::template->process("MyTemplate.atml", $::vars)
|| $::template->process("MyErrorTemplate.atml", $::vars)
|| MyExceptionHandlingFunction($::template->error());
Your MyExceptionHandlingFunction() will only be called when the
MyErrorTemplate fails.
You could also pass the error into the second stash with something
like:
$::template->process("MyTemplate.atml", $::vars)
|| $::template->process("MyErrorTemplate.atml", {
%{$::vars},
error => $::template->error()
})
|| MyExceptionHandlingFunction($::template->error());
...or...
2) Apply the patch below. This causes compile errors of the top-level
template to be processed in the usual manner (ie: try to dispatch
via the ERROR config). This seems to correctly handle the case
where the ERROR template also has a syntax error: it doesn't go
into an infinite loop; rather $template->process() returns with
an error about the ERROR template.
Craig
--- Template-Toolkit-2.04c/lib/Template/Service.pm.orig Sat Aug 4 03:30:11 2001
+++ Template-Toolkit-2.04c/lib/Template/Service.pm Wed Aug 8 22:48:07 2001
@@ -65,8 +65,10 @@
# pre-request compiled template from context so that we can alias it
# in the stash for pre-processed templates to reference
eval { $template = $context->template($template) };
- return $self->error($@)
- if $@;
+ if ($error = $@) {
+ return $procout if ( defined($procout = $self->_recover(\$error)) );
+ return $self->error($error);
+ }
# localise the variable stash with any parameters passed
# and set the 'template' variable