Web Programming TutorialsError Handling in Zend Framework

Error Handling in Zend Framework

Error Handling in Zend FrameworkIn our previous session of Zend Framework we learnt about concept of Autoloading, today let us go through the error handling in Zend Framework.

  • In zend framework a Front Controller is the first which receives the requests, as a result, all the exceptions arrive to front controller only allowing the developer to handle the error there itself.
  • By default, the Zend_Controller_Front aggregates the exceptions in the response object.
  • Several mechanisms are already built in MVC to handle errors:
  • By default the error handler plugin is registered and used. This plugin is designed to handle,
    1. Errors due to missing controllers or actions
    2. Errors occurring within action controllers
  • Exceptions can also be handled by a function throwExceptions() of Zend_Controller_Front.
  • If we pass a Boolean TRUE value in this throwExceptions() method, we can tell the front controller that instead to gathering all the exceptions in the response object or using the error handler plugin, we would rather handle them ourselves, as shown below using try- catch:
  • $fr->throwExceptions(true);
    try
    {
    	$fr->dispatch();
    }
    catch(Exception $s)
    {
    	//handle exception here
    }
    
  • Next we know that by default Zend_Controller_Front aggregates the exceptions in the response object. So we can use Zend_Controller_Response_Abstract::renderExceptions() method to render the exceptions stored in it.
  • If we pass a boolean value TRUE to renderExceptions() method, we can tell the response object to render the error messages and backtrace when rendering itself. In this case the exceptions raised by our applications will be raised.
  • We can also handle exceptions by using Zend_Controller_Front::returnResponse() and Zend_Controller_Response_Abstract::isException().
  • Here if we pass a Boolean value TRUE to returnResponse() method, the Zend_Controller_Front::dispatch() method will not render the response, but instead return it.
  • Once we get the response, we can then even test to see if any exceptions are trapped using its isException() method and retrieve the exceptions via its getException() method. An example is shown below:
  • $fr->returnResponse(true);
    $resp = $fr->dispatch();
    if ($resp->isException()) 
    {
        $excep = $resp->getException();
        // handle exceptions here
    } 
    else 
    {
        $resp->sendHeaders();
        $resp->outputBody();
    }
    
  • The primary advantage of this method over Zend_Controller_Front::throwExceptions() method is, to allow us to conditionally render the response after handling the exception.
  • Since the error handling in Zend Framework is typically done using the error controller, we can even allow it to handle errors by passing a FALSE value to throwExceptions() method of front controller as shown below:
  • $fr->throwExceptions(false);
  • Now write an action controller class for handling errors as shown below as an example:
  • class SomeController extends Zend_Controller_Action
     {
        public function someAction() 
       {
           if($this->_getParam(‘parameter’,false)) 
    	{
          	//parameter present
        	}
        else 
    	{
          //parameter not present
          throw new ParameterNotFoundException(Parameter is missing!');
        }
      }
    }
    
  • Here if we don’t find the parameter, we throw the ParameterNotFoundException. Here by doing this, we allow the normal error handling code of the framework to take over the control, where the error is caught and the request is forwarded to the error controller.

Thus we studied about error/exception handling in this Error Handling in Zend Framework tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -