1

我有一个控制器,我在其中抛出一个自定义异常,并且我有一个自定义异常渲染器类,它扩展了基本异常渲染器。

现在,当我抛出异常时,我想对出错的东西进行一些清理,然后呈现自定义错误页面。

class AppExceptionRenderer extends ExceptionRenderer {

    public function invalidCall($error) {
        $this->controller->render('/Errors/invalid_call');
        $this->controller->response->send();
    }

    public function incompleteCall($error) {
        $this->controller->render('/Errors/incomplete_call');
        $this->controller->response->send();
    }
}

到目前为止,渲染效果很好。但是我应该把清理的逻辑放在哪里呢?在异常本身?在渲染器中?在控制器抛出异常之前?

4

1 回答 1

1

好吧,通常有很多方法可以给猫剥皮,但我想说,为了保持干燥,便于测试,并且为了符合推荐的胖模型概念,你应该把逻辑放在模型。

为了解耦清理和异常处理,您可以例如利用事件系统并让可能需要清理的模型将自己附加为侦听器(他们应该最清楚自己是否需要清理),并让自定义错误处理程序调度适当的事件,这样异常处理程序不需要了解应用程序内部。

下面是一些非常基本的、未经测试的示例代码,可以说明这个想法:

<?php
App::uses('CakeEventManager', 'Event');

class ExampleModel extends AppModel
{
    public $name = 'Example';

    public function __construct($id = false, $table = null, $ds = null)
    {
        CakeEventManager::instance()->attach(array($this, 'cleanup'), 'AppErrorHandler.beforeHandleException');
        parent::__construct($id, $table, $ds);
    }

    public function cleanup()
    {
        // do some magic
    }
}
?>

<?php
App::uses('CakeEvent', 'Event');
App::uses('CakeEventManager', 'Event');

class AppErrorHandler extends ErrorHandler
{
    public static function handleException(Exception $exception)
    {
        CakeEventManager::instance()->dispatch(new CakeEvent('AppErrorHandler.beforeHandleException', get_called_class(), array($exception)));
        parent::handleException($exception);
    }
}
?>

更新

为了能够仅对特定异常做出反应,您可以例如在事件名称中使用异常类名称,因此它会触发...beforeHandleFooBarException您可以显式订阅的事件:

<?php
class AppErrorHandler extends ErrorHandler
{
    public static function handleException(Exception $exception)
    {
        CakeEventManager::instance()->dispatch(new CakeEvent('AppErrorHandler.beforeHandle' . get_class($exception), get_called_class(), array($exception)));

        parent::handleException($exception);
    }
}
?>

<?php
class ExampleModel extends AppModel
{
    public $name = 'Example';

    public function __construct($id = false, $table = null, $ds = null)
    {
        $eventManager = CakeEventManager::instance();
        $callback = array($this, 'cleanup');

        $eventManager->attach($callback, 'AppErrorHandler.beforeHandleInvalidCallException');
        $eventManager->attach($callback, 'AppErrorHandler.beforeHandleIncompleteCallException');

        parent::__construct($id, $table, $ds);
    }

    public function cleanup()
    {
        // do some magic
    }
}
?>

如果您坚持使用通用异常事件,那么另一种选择是在模型事件侦听器回调中检查异常的类型:

public function __construct($id = false, $table = null, $ds = null)
{
    CakeEventManager::instance()->attach(array($this, 'beforeHandleException'), 'AppErrorHandler.beforeHandleException', array('passParams' => true));
    parent::__construct($id, $table, $ds);
}

public function beforeHandleException($exception)
{
    if($exception instanceof InvalidCallException ||
       $exception instanceof IncompleteCallException)
    {
        $this->cleanup();
    }
}

public function cleanup()
{
    // do some magic
}
于 2013-06-24T20:25:12.533 回答