如何在 Zend Framework 操作中捕获我自己的模型异常

发布于 2024-10-04 04:11:53 字数 1993 浏览 4 评论 0原文

我有一个抛出 XMLDocument_Exception 的模型。在我的控制器操作中,我想包含一个 try catch 块,它可能会抛出此异常,但由于某种原因它没有被捕获。

public function listissuesAction() {
    $publicationPid  = $this->_request->get('publicationPid');
    $this->getFrontController()->throwExceptions(true);
    try {
        $fedoraPublication = new FedoraMETSPublication($publicationPid);
    } catch (XMLDocument_Exception $e) {
        return $this->_forward('content/unavailable');
    }
}

并且模型抛出异常,如下所示:

if ($mets) {
    $this->loadXML($mets);
} else {
    throw new XMLDocument_Exception('Failed to load METS Document from SOAP Server');
}   

FedoraMETSPublication 扩展了 FedoraMETS,后者扩展了 XMLDocument。上面的代码位于 FedoraMETS 构造函数的构造函数中。

我看到以下错误:

Fatal error:  Uncaught exception 'XMLDocument_Exception' with message 'Failed to load METS Document from SOAP Server' in    2 /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS.php:21
Stack trace:
#0 /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS/Issue.php(16): FedoraMETS->__construct('llgc-id:1183037')
#1 /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS/Publication.php(44): FedoraMETSIssue->__construct('llgc-id:1183037')
#2 /Users/dof/Sites/ccymod/trunk/application/views/scripts/browse/listissues.phtml(19): FedoraMETSPublication->getIssues()
#3 /Users/dof/Sites/ccymod/trunk/lib/Zend/View.php(108): include('/Users/dof/Site...')
#4 /Users/dof/Sites/ccymod/trunk/lib/Zend/View/Abstract.php(831): Zend_View->_run('/Users/dof/Site...')
#5 /Users/dof/Sites/ccymod/trunk/lib/Zend/Controller/Action/Helper/ViewRenderer.php(903): Zend_View_Abstract->render('browse/listissu...')
#6 /Users/dof/Sites/ccymod/trunk/lib/Zend/Controller/Action/Helper/ViewRenderer.php(924): Zend_Controller_Action_Helper_ViewRend in /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS.php on line 21

为什么我没有在操作控制器中捕获此异常?

I have a model which throws a XMLDocument_Exception. In my controller action I want to include a try catch block which may throw this exception but for some reason it is not getting caught.

public function listissuesAction() {
    $publicationPid  = $this->_request->get('publicationPid');
    $this->getFrontController()->throwExceptions(true);
    try {
        $fedoraPublication = new FedoraMETSPublication($publicationPid);
    } catch (XMLDocument_Exception $e) {
        return $this->_forward('content/unavailable');
    }
}

and the model is throwing the exception like so:

if ($mets) {
    $this->loadXML($mets);
} else {
    throw new XMLDocument_Exception('Failed to load METS Document from SOAP Server');
}   

FedoraMETSPublication extends FedoraMETS which extends XMLDocument. The code above is in the constructor for FedoraMETS's constructor.

I'm seeing the following error:

Fatal error:  Uncaught exception 'XMLDocument_Exception' with message 'Failed to load METS Document from SOAP Server' in    2 /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS.php:21
Stack trace:
#0 /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS/Issue.php(16): FedoraMETS->__construct('llgc-id:1183037')
#1 /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS/Publication.php(44): FedoraMETSIssue->__construct('llgc-id:1183037')
#2 /Users/dof/Sites/ccymod/trunk/application/views/scripts/browse/listissues.phtml(19): FedoraMETSPublication->getIssues()
#3 /Users/dof/Sites/ccymod/trunk/lib/Zend/View.php(108): include('/Users/dof/Site...')
#4 /Users/dof/Sites/ccymod/trunk/lib/Zend/View/Abstract.php(831): Zend_View->_run('/Users/dof/Site...')
#5 /Users/dof/Sites/ccymod/trunk/lib/Zend/Controller/Action/Helper/ViewRenderer.php(903): Zend_View_Abstract->render('browse/listissu...')
#6 /Users/dof/Sites/ccymod/trunk/lib/Zend/Controller/Action/Helper/ViewRenderer.php(924): Zend_Controller_Action_Helper_ViewRend in /Users/dof/Sites/ccymod/trunk/application/models/Fedora/METS.php on line 21

Why am I not catching this exception in my Action Controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明天过后 2024-10-11 04:11:53

检查堆栈跟踪,#2:

#2 /Users/dof/Sites/ccymod/trunk/application/views/scripts/browse/listissues.phtml(19): FedoraMETSPublication->getIssues()

显然,异常被抛出到视图,而不是控制器。在视图中使用 try...catch (或更改您的方法)。

Check the stack trace, #2:

#2 /Users/dof/Sites/ccymod/trunk/application/views/scripts/browse/listissues.phtml(19): FedoraMETSPublication->getIssues()

Apparently, the exception is being thrown to the view, not to the controller. Use try...catch in the view (or change your approach).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文