symfony 1.4:如何将异常消息传递给error.html.php? [英] symfony 1.4: How to pass exception message to error.html.php?

查看:139
本文介绍了symfony 1.4:如何将异常消息传递给error.html.php?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用这里描述的特殊变量 $ message http://www.symfony-project.org/cookbook/1_2/en/error_templates ,但似乎这个变量在symfony 1.4中没有定义,至少它不包含消息以这种方式传递给异常 throw new sfException('some message')

I tried using special variable $message described here http://www.symfony-project.org/cookbook/1_2/en/error_templates but it seems this variable isn't defined in symfony 1.4, at least it doesn't contain message passed to exception this way throw new sfException('some message')

你知道其他方式传递这个消息到error.html.php?

Do you know other way to pass this message to error.html.php ?

推荐答案

您需要执行一些自定义错误处理。我们自己实施了一个自定义的symfony动作。但是请谨慎操作,此操作本身也可能引发异常,您需要考虑到这一点。

You'll need to do some custom error handling. We implemented a forward to a custom symfony action ourselves. Be cautious though, this action itself could be triggering an exception too, you need to take that into account.

以下可能是一个好的开始。首先为事件添加一个监听器,一个好的地方是ProjectConfiguration.class.php:

The following might be a good start. First add a listener for the event, a good place would be ProjectConfiguration.class.php:

$this->dispatcher->connect('application.throw_exception', array('MyClass', 'handleException'));

使用事件处理程序可能足以满足您要处理异常的情况,例如,如果您只是希望将堆栈跟踪邮件发送给管理员。我们希望转到自定义操作来显示和处理反馈表单。我们的事件处理程序看起来像这样:

Using the event handler might suffice for what you want to do with the exception, for example if you just want to mail a stack trace to the admin. We wanted to forward to a custom action to display and process a feedback form. Our event handler looked something like this:

class MyClass {
  public static function handleException(sfEvent $event) {
    $moduleName = sfConfig::get('sf_error_500_module', 'error');
    $actionName = sfConfig::get('sf_error_500_action', 'error500');
    sfContext::getInstance()->getRequest()->addRequestParameters(array('exception' => $event->getSubject()));
    $event->setReturnValue(true);
    sfContext::getInstance()->getController()->forward($moduleName, $actionName);
  }
}

您现在可以将模块和操作配置为转发到在settings.yml中的例外

You can now configure the module and action to forward to on an exception in settings.yml

all:
  .actions:
    error_500_module:       error
    error_500_action:       error500

在动作本身中,您可以随意执行任何您想要的异常,例如。显示反馈表单以联系管理员。您可以使用$ request-> getParameter('exception')

In the action itself you can now do whatever you want with the exception, eg. display the feedback form to contact the administrator. You can get the exception itself by using $request->getParameter('exception')

这篇关于symfony 1.4:如何将异常消息传递给error.html.php?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆