Throwing exceptions from a model / view / controller in a Zend Framework application

In the Zend Framework library, the current practice (around 1.10.8) is that library components throw exceptions that extend Zend_Exception .

eg. the Zend_Layout component throws a Zend_Layout_Exception

In my own ZF library, where I add my own ZF components or extend existing components, I throw a Mylibrary_Exception (this is not really called that :)

I see that they will change some of them in ZF 2.0

http://framework.zend.com/wiki/display/ZFDEV2/Proposal+for+Exceptions+in+ZF2

My actual question is this:

In my MVC application in my controllers / models / views, if I need to throw an exception (and this will be rare, because obviously I will handle the expected errors differently) - BUT if I really need to throw an exception here, which is best practice in zf?

should i just

 throw new Exception("this is an exception"); 

or should I create exception classes in my ZF modules, similar to how the ZF library is organized. that is, they have exception classes for each library component, should I have exception classes for each application module?

applications / modules / user / controllers / UserController.php

applications / modules / user / form / UserForm.php

application / modules / user / model / User.php

applications / modules / user / view / scripts / index.phtml

application / modules / user / exceptions / Exception.php (class User_Exception)

application / modules / user / exceptions / SuperexampleException.php (class User_Exception_Superexample)

I have never seen anyone do something like this before in ZF, so I'm not sure if this is a good idea.

UPDATE

To clarify my question further - when creating exceptions in the part of the MVC application (as opposed to a library) - are there any conventions regarding the use of specific exception classes (for example, a library), but only using the general class Exception?

+4
source share
2 answers

I suggest having some โ€œgeneralโ€ exceptions (InvalidParameter, InvalidRange). A good starting point is a Czech structure called Nette .

Then add some application / user / action specific exceptions - for example

  • InvalidUserInputException - for states in which the user entered an invalid value (for example, "-1" as an amount in eshop)
  • NotFoundException - for states in which something is not found
  • and etc.

Add special member variables to these exceptions โ€” for example, identifier or data โ€” and use them to use exceptions as delivery of error messages.

 $e = new NotFoundException('There is no user %s in here!'); $e->setUser('Frank'); throw $e; // later in error controller if ($e instanceof NotFoundException) { // set header to 404, display error message, etc } 

It's good that you can extend exceptions anyway, and you will catch them in the error controller (even better, using interfaces)

 class AccessDeniedExceptionimplements ILogableException //... throw new AccessDeniedException(); //in EC if ($e instanceof ILoggableException) { $this->getLogger()->log($e->getLogMessage()); } 
+4
source

ZF V1 exception throwing handled by default error controller

  $errors = $this->_getParam('error_handler'); switch ($errors->type) { case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE: case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER: case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: // 404 error -- controller or action not found $this->getResponse()->setHttpResponseCode(404); $this->view->message = 'Page not found'; break; default: // application error $this->getResponse()->setHttpResponseCode(500); $this->view->message = 'Application error'; break; } 

and if you need to catch a custom exception, you need to add them to the error controller it is easy and convenient, I like it and I am very pleased with it.

0
source

All Articles