如何在 Template::Toolkits CATCH 块中发出警告?
我正在处理的代码在 Template::Toolkit
模板中有一堆 TRY
/CATCH
块。它们看起来像这样:
[% TRY; x = OBJ.method(data); CATCH; "<!-- error: $error -->"; END %]
从两个角度来看,这都是不好的。首先,错误被插入到交给用户的 HTML 中,其次,开发人员很难发现错误。在我看来,所有错误都应该记录到同一个错误日志中。现在我通过 warn
函数来做到这一点。我已将上面的代码更改为,
[% TRY %]
[% x = OBJ.foo(data) %]
[% CATCH %]
[% RAWPERL %]
warn "error calling method foo on a bar object: " . $stash->get("error");
[% END %]
[% END %]
但这对于本应简单的事情来说感觉太冗长了。有没有更好的方法来做到这一点我不知道?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好主意!我自己从来没有想过,但现在会为我自己的系统实现解决方案。
而且开箱即用! stderr 过滤器将块的输出打印到 STDERR:
不需要 MVC,不需要代码,只是更好的生活。
一种更高级的方法是在控制器中创建一个对象,其工作是记录错误,以便它可以更智能地处理它们:
[% logger.warn('Big Problem') %]
它可以通过电子邮件发送它们,将将它们记录到日志中,或者向您不喜欢的开发人员发送短信。 ;-)
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:
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. ;-)