如何在Zend中正确设置异常处理程序? [英] How to correctly set up exception handler in Zend?

查看:76
本文介绍了如何在Zend中正确设置异常处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Zend(RESTful)中的几个控制器重新定义异常处理程序。

I'm trying to redefine exception handler for a couple of my controllers 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();
    }
}

但是出于某些原因,Zend使用默认模板/错误处理而我的故障函数没有执行。
顺便说一句,我正在使用 module 架构。该控制器来自 rest 模块。Zend的默认错误处理程序来自 default 模块。

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.

推荐答案

这是一个有趣的问题。目前我还不太确定,因此我将对此进行一点研究,然后看看我的想法。目前,有些解决方法也不是贫民窟。一种方法是创建一个抽象控制器,从中扩展您的rest模块中的所有控制器。

This is an interesting question. I'm not totally sure at the moment so I'm going to research this one a bit and see what I come up with. For now there are workarounds that aren't too ghetto either. One way would be to create an abstract controller from which to extend all of your controllers in your rest 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
}

此外,我发现了这篇有趣的文章: http://zend-framework-community.634137.n4.nabble。 com /处理未捕获的异常并使用设置异常处理程序在Zend-Framework-td1566606.html

Also, I found this interesting article: http://zend-framework-community.634137.n4.nabble.com/Dealing-with-uncatched-exceptions-and-using-set-exception-handler-in-Zend-Framework-td1566606.html

编辑:

在引导文件中的某些位置,您需要添加以下内容:

Somewhere in your bootstrap file you will need to add this:

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

第一行应该有效地关闭Zend的异常处理,唯一缺少的是控制结构确定当前请求是否针对您的REST服务。 注意必须在Bootstrap.php文件中进行操作的原因是,从未调用init()函数中对set_exception_handler()的调用,因为Zend Framework先抛出了 first 。将其放在引导文件中将对此进行解决。

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.

这篇关于如何在Zend中正确设置异常处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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