Handling errors in Zend Framework

Tags:

In Zend Framework based applications, error handling is typically done using the error controller, but there are different ways to send the execution to it – some better than others.

Let’s look at some ways to trigger the error controller and how to have it handle different error conditions.

Error controller structure

Let’s quickly cover the basics of the error controller first:

  • To enable the error controller, you need to set throwExceptions to false in the front controller:
    $fc->throwExceptions(false);
  • The Zend Framework Reference Guide has a chapter on the Error Controller where you can learn the basics

Now, how to structure your error controller?

The easiest way out is, of course, to just handle everything in the errorAction method. However, it will be easier to understand the controller, if you separate different error types into separate actions. This also allows us to easily have error-specific code without bloating the errorAction method too much, in addition to allowing us to easily render error-specific view scripts.

class ErrorController extends Zend_Controller_Action {
  public function errorAction() {
    $error = $this->_getParam('error_handler');
    switch(get_class($error->exception)) {
      case 'PageNotFoundException':
        $this->_forward('page-not-found');
        break;
 
      case 'NotAuthorizedException':
        $this->_forward('not-authorized');
        break;
 
      default:
        //put some default handling logic here
        break;
    }
  }
 
  public function pageNotFoundAction() {
    //goes here if the page was not found
  }
 
  public function notAuthorizedAction() {
    //goes here if the user has no access to a page
  }
}

So in the above example, the errorAction would be used to determine the type of the error, and then the request would be forwarded to the respective action. Errors that don’t have a specific action for them can have some generic handling in errorAction.

Triggering the error controller

The most common approach to trigger the error controller is to simply forward the request to it in an erroneous condition:

class SomeController extends Zend_Controller_Action {
  public function someAction() {
    if($this->_getParam('foo',false)) {
      //parameter present - ok
    }
    else {
      //parameter not present - error
      $this->_forward('some-error', 'error');
    }
  }
}

While simple, this approach has a small flaw: The forwarding code is essentially creating a coupling between this controller and the error controller!

If you want to make your code easily re-usable, this is a no-no. By forwarding to a specific action, you are requiring anyone who wants to use the code to implement an error controller with the actions your code requires.

A better approach would be to use exceptions. Instead of forwarding, you throw an exception:

class SomeController extends Zend_Controller_Action {
  public function someAction() {
    if($this->_getParam('foo',false)) {
      //parameter present - ok
    }
    else {
      //parameter not present - error
      throw new ParameterNotFoundException('oh no!');
    }
  }
}

This time we are throwing a custom exception called ParameterNotFoundException. By doing this, we allow the normal error handling code of the framework to take over: the error is caught, and the request is forwarded to the error controller.

Now you can add some code in the error controller to handle this kind of exceptions. Or maybe your project does not need custom handling for this exception, so you can simply use the generic handling code for it.

In closing

There’s not much to error handling in ZF thanks to the ready components that make it really easy. Just throw an exception, and write some code in the error controller to handle it. As a rule of thumb, you should at least log the exceptions that get as far as the error controller!

Further reading:
There are lots of other posts about Zend Framework in this blog