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?
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. ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With