0

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. I'll throw the error from the controller then. My Articles model is pretty complex. What should I do for simpler models. Models that can only throw an error when the content has been deleted. That's not really worth creating a special exception class right? Or should I create a isValid() kind of function in the model? And include that in all controllers/actions that try to load something. Seems like a lot of extra code...
If it's worth creating new exception class is up to you :) However, take a look around existing Zend exceptions, SPL Exceptions (php.net/manual/en/spl.exceptions.php) or you may throw an ordinary Exception. As for validating it, generally you may just throw an exception within model, but be sure that method which throws an exception is executed within try-catch block. Java has that sorted better, since method, whose result may be an exception must be declared with 'throws My_Kind_Of_Exception'.
0

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.

2 Comments

You sure I can forward and/or set response codes in the exception model? If so, might be I can make some general exceptions that can occur in multiple models. Also; where do I place the exception code? Just in the models folder?
Have a look at the documentation for Zend_Controller_Plugin_ErrorHandler and the way the error controller is configured to turn specific exception codes into 404 errors. As for your model exception, you'd create a folder called Articles where your Articles.php class resides, and place the code in Exception.php; that way the autoloader can pick it up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.