I'm not actually catching anything. I am leaving that to the front controller.
It is the exception in _validateAcceptHeader() that is not being caught while
the exception in _validateHasId() is caught. Strangely, if I move the exception
in _validateHasId() to any other place in that method, for instance above
setting $affectedActions, the exception is not caught. Also, if I remove the
in_array() from the condition leaving only !$id it is not caught but if I leave
the in_array() and remove the !$id it is caught.
Also, I have not set the front controller's throwExceptions to true. It should
be false by default and just for good measure I explicitly set it to false.
This did not change anything.
Anyways, I'm at a loss. Any help would be appreciated.
public function preDispatch()
{
$this->_validateAcceptHeader();
$this->_validateHasId();
}
protected function _validateAcceptHeader()
{
if(!$this->acceptsXml() && !$this->acceptsJson()) {
throw new \Exception('Only JSON and XML are allowed for Accept header.', 406);
}
}
protected function _validateHasId()
{
$affectedActions = array(
'put',
'delete'
);
$request = $this->getRequest();
$action = $request->getActionName();
$id = $request->getParam('id', false);
if(in_array($action, $affectedActions) && !$id) {
throw new \Exception('An ID is required.', 400);
}
}
Mark
--
Have fun or die trying - but try not to actually die.
On Thursday, March 1, 2012 at 12:32 PM, Andreas Möller wrote:
> > I have a controller plugin with a preDispatch() method. It calls two very
> > basic methods and both of them can throw an exception. Right now they are
> > both throwing an instance of Exception. The problem is that when one of
> > them throws an exception it gets caught and added to the response object.
> > When the other throws an exception it is not caught and triggers a fatal
> > error. I really don't understand why one would be caught and the other
> > isn't. Any ideas?
>
>
>
> Maybe you need to be less specific in your catch statement.
>
> Compare
>
> try {} catch (\Exception $e) {}
>
> with
>
> try {} catch (\Zend_Auth_Exception $e) {}
>
> But, without the code, it's hard to tell.
>
>
> Andreas
--
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]