Erik
Erik

Reputation: 559

Zend framework how to handle exceptions

What is the best practice for throwing an Exception in the following situation:

My URL structure is: /articles/view/id/1

My Controller: AticlesController

And this controller loads a Mapper model: Application_Model_Mapper_Articles

In this model I aggregate all kinds of data. I check several criteria in this model to decide between throwing a 404 error or showing the article.

If I decide to throw the error. How should I do it?

My first guess was throwing it from within the model. But there is no default 'Model_Exception' class and there is a 'Zend_Controller_Action_Exception'. Should I just throw this exception from within the model? Or should I pass the error message back from the model to the controller and then throw the error?

Upvotes: 0

Views: 822

Answers (2)

usoban
usoban

Reputation: 5478

Model itself should not interfere with the FrontController, so yes, throw the exception from the controller.

You may throw an exception from the model (not Zend_Controller_Action_Exception, but some that more precisely describes your problem), catch it in the controller and on that base decide if you're gonna throw Zend_Controller_Action_Exception or not.

Upvotes: 3

cmbuckley
cmbuckley

Reputation: 42547

You could create an Application_Model_Mapper_Articles_Exception, and you could either choose to handle that in your controller and throw exceptions to the error controller, or use your exception code to designate the type of HTTP response code to set.

Upvotes: 0

Related Questions