Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I throw a warning in Template::Toolkits CATCH block?

The code I am working on has a bunch of TRY/CATCH blocks in Template::Toolkit templates. They look like this:

[% TRY; x = OBJ.method(data); CATCH; "<!-- error: $error -->"; END %]

This is bad from two perspectives. First, the error is being inserted into the HTML handed to the user, and second, the error is hard to find for the developers. In my opinion, all errors should be logged to the same error log. Right now I do that through the warn function. I have changed the code above to be

[% TRY %]
    [% x = OBJ.foo(data) %]
[% CATCH %]
    [% RAWPERL %]
        warn "error calling method foo on a bar object: " . $stash->get("error");
    [% END %]
[% END %]

but this feels far too verbose for what should be a simple thing. Is there some better way I am ignorant of to do this?

like image 200
Chas. Owens Avatar asked Sep 13 '11 14:09

Chas. Owens


1 Answers

Great idea! Never thought of it myself, but will implement the solution for my own system now.

And it is possible out of the box! The stderr filter prints output of a block to STDERR:

[% FILTER stderr %]  
   Found a big problem
[% END %]

No MVC required, no code, just a better life.

A more advanced way to do it is to create an object within your controller whose job it is to log errors, so it can process them more intelligently:

[% logger.warn('Big Problem') %]

It could email them, put them into the log, or SMS to the developer you don't like. ;-)

like image 60
KateYoak Avatar answered Oct 20 '22 20:10

KateYoak