Surfrdan
Surfrdan

Reputation: 504

How to catch my own Model Exceptions in a Zend Framework Action

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?

Upvotes: 0

Views: 593

Answers (1)

netcoder
netcoder

Reputation: 67695

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).

Upvotes: 1

Related Questions