Is it possible to have a custom 500 error page for zend framework? I mean something in the ErrorController…to see a custom view render if you have a 500 Error.
<?php
class ErrorController extends Zend_Controller_Action
{
private $_notifier;
private $_error;
private $_environment;
public function init()
{
parent::init();
$this->_error = $this->_getParam('error_handler');
}
public function errorAction()
{
switch ($this->_error->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
$this->getResponse()->setHttpResponseCode(404);
$this->view->message = 'Page not found';
break;
default:
//Doesn't work
$this->renderScript('error/500.phtml');
$this->getResponse()->setHttpResponseCode(500);
break;
}
}
}
Yes, it’s completely possible, but it depends on the reason of why you would end up with a status 500 in the first place.
Anything that results in the Apache webserver emitting this status cannot be intercepted by your ErrorController in an easy way.
But if you decide a certain action has to be exited with a HTTP response code 500, you are free to do so.
One important thing is: If you throw an exception that is not catched, the Zend Framework will catch it and redirect to the error controller. This exception object is stored as the property “exception” within the “error_handler” parameter. So in your code you should be able to just ask if you are there because of an uncatched exception:
Personally I decided that my controllers are able to throw Controller_Exception which carries the HTTP status code that has to be emitted. Only the controller should decide what happens on the response side if stuff is wrong, if you need the detailed control of response codes.
About logging: Log errors where they happen. If something makes you throw an exception, the correct location for logging is right before the throw, I’d say. Of course you can always log in the error controller as well, to signal that the error that once might only be at warning level really did not get caught and really is an error, but this might not really tell you where stuff went wrong.