English 中文(简体)
如何在泽德正确设立例外手稿?
原标题:How to correctly set up exception handler in Zend?

我试图重新界定泽德(RESTful)一对手控制器的例外情况。

这是我的法典:

abstract class RestController extends Zend_Rest_Controller
{
    public function init()
    {
        set_exception_handler(array($this,  fault ));
    }

    public function fault($exception = null, $code = null)
    {
       echo $exception->getMessage();
    }
}

But for some reason Zend uses default template/error handling and my fault function didnt execute. Btw, I m using module architecture. that controller is from rest module.. Zend s default error handler is from default module.

最佳回答

这是一个令人感兴趣的问题。 现在我并不完全相信,这样我就能够研究这个问题,看看我所看到的情况。 如今,有些工作环绕太了贫民窟。 一种办法是建立一个抽象的控制器,从中将你的控制器扩展到其余模块。

abstract class RestAbstractController extends Zend_Rest_Controller
{
    final public function __call($methodName, $args)
    {
        throw new MyRestException("Method {$methodName} doesn t exist", 500);
    }
}

// the extends part here is optional
class MyRestException extends Zend_Rest_Exception
{
    public function fault($exception = null, $code = null)
    {
        echo $exception->getMessage() .     . __CLASS__;
        exit;
    }
}

class RestController extends RestAbstractController
{
    // method list
}

另外,我发现这一有趣的文章:

Edit:

在座boot中,你需要补充:

$this->_front->throwExceptions(true);
$ex = new MyRestException();
set_exception_handler(array($ex,  fault ));

The first line there should effectively turn off Zend s exception handling, the only thing missing is a control structure to determine if the current request is for your REST service or not. NOTE The reason this had to go in the Bootstrap.php file was that your call to set_exception_handler() in the init() function was never reached because Zend Framework threw the exception first. Placing that in the bootstrap file will counter that.

问题回答

Finally solved the problem by myself :)

http://framework.zend.com/manual/en/zend. Controller. Tenions.html”rel=“nofollow” 重要文件:

Zend_Controller_Front::throwExceptions()

By passing a boolean TRUE value to this method, you can tell the front controller that instead of aggregating exceptions in the response object or using the error handler plugin, you d rather handle them yourself

因此,正确的解决办法是:

abstract class RestController extends Zend_Rest_Controller
{
    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $front->throwExceptions(true);

        set_exception_handler(array($this,  fault ));
    }

    public function fault($exception = null, $code = null)
    {
       echo $exception->getMessage();
    }
}

我们只是要补充一点。

$front = Zend_Controller_Front::getInstance();
$front->throwExceptions(true);

http://www.un.org。





相关问题
Separating Business Layer Errors from API errors

The title is horrible, i know; I m terrible at titles on SO here. I m wondering what would be the best way to present unified error responses in a webapi when errors could be raised deep inside the ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

How to tell why a file deletion fails in Java?

File file = new File(path); if (!file.delete()) { throw new IOException( "Failed to delete the file because: " + getReasonForFileDeletionFailureInPlainEnglish(file)); } Is there a ...

Exceptions: redirect or render?

I m trying to standardize the way I handle exceptions in my web application (homemade framework) but I m not certain of the "correct" way to handle various situations. I m wondering if there is a ...

热门标签