Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Catching Template Toolkit Errors

by Herkum (Parson)
on Mar 19, 2007 at 02:45 UTC ( #605410=perlquestion: print w/replies, xml ) Need Help??

Herkum has asked for the wisdom of the Perl Monks concerning the following question:

I am developing a web site with Template Toolkit to generate web pages. The problem I am having is that when there are errors trying to generate the web page, It will not display an errors in the log. I have an 'Error' run_mode (provided through CGI::Application and CGI::Application::Plugin::TT but that just displays a generic error template.

Can any give me a hint on how to force Template errors to show up in the error_log or in a variable that I can use to make it show up in the error template? It is driving me crazy to try and debug templates without error messages.

Replies are listed 'Best First'.
Re: Catching Template Toolkit Errors
by GrandFather (Saint) on Mar 19, 2007 at 03:02 UTC

    What have you tried?

    Do you have:

    use CGI::Carp qw(fatalsToBrowser); # Remove for production code

    I use an error reporting sub for reporting "standard" errors and dumping the CGI parameter list:

    sub showError { my $err = shift; print header(); print start_html("IsosimCGI_Error"); print h1("IsosimCGI Error"); print p ($err); my %params = Vars (); for my $param (keys %params) { print br ("$param -> $params{$param}"); } print end_html(); exit 1; }

    Have you tried wrapping your troublesome code in an eval:

    eval { #troublesome stuff here }; ShowError ($@) if $@;

    DWIM is Perl's answer to Gödel
Re: Catching Template Toolkit Errors
by Zaxo (Archbishop) on Mar 19, 2007 at 04:42 UTC

    In cgi, a message to STDERR will generally show up in the httpd's error log. For non-fatal errors, Perl's warn will do the job. For fatal ones, die.

    You can keep error messages in a variable by opening STDERR to a reference to that variable. Something like:

    { my $errors; open local(*STDERR), '>>', \$errors or die $!; # do stuff, can call external functions that warn or die if ($errors) { print error_template($errors); # however you do that exit 0; # or non-zero if we want to tell the server we failed } # maybe do more stuff - success branch }

    After Compline,
    Zaxo

Re: Catching Template Toolkit Errors
by rinceWind (Monsignor) on Mar 19, 2007 at 07:37 UTC

    How are you using Template Toolkit? Maybe you are ignoring errors that you are getting. You haven't posted any code, so I cannot see whether this is the case.

    In the manual are examples that check the return value from process and die with the error method when unsuccessful. This will result in diagnostics on stderr. Is your code doing this?

    --
    Apprentice wetware hacker

      I looked at the piece of code that you mentioned and maybe part of the problem is that I am using cpan:://CGI::Application::Plugin::TT The plugin does not mention much in reference to Template errors except that I can have an 'Error' runmode to display the an error template.

      I was hoping there was a variable that could be set when initializing the Template configuration that would force the template to die with the error message or stick the error in a value that is accessible by the template.

      Update: I am also wondering if might have to do with the way CGI::Application handles carp calls on a Win32 box. I looked at the source code for the plugin and it calls croak which, I don't think should be a problem, but I am not sure how it is being handled.

      Update 2: Evidently CGI::Application does its own eval on executed code and sticks in the $@ variable and will call the error runmode. I cannot wait to get home to see if this works.

Re: Catching Template Toolkit Errors
by astroboy (Chaplain) on Mar 19, 2007 at 21:34 UTC
    I'm not sure I understand you problem. In CGI::App, the error is passed as a parameter to your error runmode:
    sub error { my $self = shift; my $error = shift; # Do something with the error - e.g. write it to a log file $self->logger->error("Warning error: $error"); #.... and then provide friendly message to user }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://605410]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2022-05-25 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?