A Useful Error Controller Class For Zend Framework Applications
Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.
23rd December 2008 - 5 minutes read time
One useful function of any application is to report on any errors that occurred. Zend Framework comes with a nice error controller system that you can activate by creating an ErrorController class.
The following is an ErrorController class that I use. It detects what sort of error occurred and displays a message to the user. It will also email a detailed report of the error to the server admins.
class ErrorController extends Zend_Controller_Action
{
public function errorAction()
{
// Ensure the default view suffix is used so we always return good
// content
$this->_helper->viewRenderer->setViewSuffix('phtml');
// Grab the error object from the request
$errors = $this->_getParam('error_handler');
switch ($errors->type) {
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';
$this->view->code = 404;
if ($errors->type == Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER) {
$this->view->info = sprintf(
'Unable to find controller "%s" in module "%s"',
$errors->request->getControllerName(),
$errors->request->getModuleName()
);
}
if ($errors->type == Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION) {
$this->view->info = sprintf(
'Unable to find action "%s" in controller "%s" in module "%s"',
$errors->request->getActionName(),
$errors->request->getControllerName(),
$errors->request->getModuleName()
);
}
break;
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$this->view->message = 'Application error';
$this->view->code = 500;
$this->view->info = $errors->exception;
break;
}
// send a mail to let someone know that there was a problem!
$config = Zend_Registry::get('configuration');
$tr = new Zend_Mail_Transport_Smtp($config->smtp);
Zend_Mail::setDefaultTransport($tr);
$mail = new Zend_Mail();
$emailBody = "An error occurred in the system. The error message contained the following output:\n\n";
$emailBody .= $this->view->message." (".$this->view->code.")\n\n";
$emailBody .= "Zend Error Type: ".$errors->type."\n\n";
$emailBody .= "REQUEST_URI: ".$_SERVER['REQUEST_URI']."\n\n";
if ( isset($_SERVER['HTTP_REFERER']) ) {
$emailBody .= "HTTP_REFERER: ".$_SERVER['HTTP_REFERER']."\n\n";
}
$emailBody .= "Stack trace: \n\n". $errors->exception->getTraceAsString()."\n\n";
// find the user to blame!
$username = Zend_Auth::getInstance()->getIdentity();
$emailBody .= "This error was created by ".$username.".";
$mail->setBodyText($emailBody);
$mail->setFrom('[email protected]', '');
$mail->addTo($config->adminEmail, $config->adminName);
$mail->setSubject('An Error Occured');
// Email
$mail->send();
$this->view->title = 'Error!';
$this->view->heading = 'Error!';
// pass the environment to the view script so we can conditionally
// display more/less information
$this->view->env = $this->getInvokeArg('env');
// pass the actual exception object to the view
$this->view->exception = $errors->exception;
// pass the request to the view
$this->view->request = $errors->request;
}
}
This error controller relies on certain options that have been set up in your configuration file. Add the following options to your configuration file to get these options working.
To enable this just call the throwExceptions() function of the Zend_Controller_Front and pass it a value of false. This will cause the framework to look for an error controller rather than try to print out things.
Finally, you need to add a view so that your errors will be displayed nicely. This will display more if the environment is set to 'test'.
Nice! The only comment I'd make is that you probably do not want emails for most 404s; I'd personally restrict the email functionality to your 500 errors.
Submitted by Matthew Weier … on Tue, 12/23/2008 - 15:12
Very true, but it depends on what sort of application you make. For the purposes that I made this for I wanted to know about 404 errors as these would definitely be mistakes.
Name
Philip Norton
Submitted by giHlZp8M8D on Tue, 12/23/2008 - 16:16
There are a few ways in which you can create complex node access systems. Modules like Taxonomy Access Control and Node Access will allow you to restrict node access in different ways, and work very well for setting up taxonomy or role based access control.
The FlashMessenger in Zend Framework has a bit of an odd name as it has nothing to do with Adobe Flash at all. It is a controller action helper defined in the class Zend_Controller_Action_Helper_FlashMessenger, which is used to store and retrive messages that are temporarily stored in the user's session.
If you have been following the last four posts you should now have an application that will allow you to view and edit PDF metadata, extract the document contents for search indexing, and allow users to search that index.
Last time we had reached the stage where we had PDF meta data and the extracted contents of PDF documents ready to be fed into our search indexing classes so that we can search them.
Comments
Submitted by Matthew Weier … on Tue, 12/23/2008 - 15:12
PermalinkSubmitted by giHlZp8M8D on Tue, 12/23/2008 - 16:16
PermalinkSubmitted by sedax on Thu, 12/25/2008 - 15:58
PermalinkAdd new comment