1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect3 id="zend.controller.plugins.standard.errorhandler">
4 <title>Zend_Controller_Plugin_ErrorHandler</title>
7 <classname>Zend_Controller_Plugin_ErrorHandler</classname> provides a drop-in
8 plugin for handling exceptions thrown by your application, including
9 those resulting from missing controllers or actions; it is an
10 alternative to the methods listed in the <link
11 linkend="zend.controller.exceptions">MVC Exceptions section</link>.
15 The primary targets of the plugin are:
20 <para>Intercept exceptions raised when no route matched</para>
24 <para>Intercept exceptions raised due to missing controllers or action methods</para>
28 <para>Intercept exceptions raised within action controllers</para>
33 In other words, the <emphasis>ErrorHandler</emphasis> plugin is designed to
34 handle <acronym>HTTP</acronym> 404-type errors (page missing) and 500-type errors (internal
35 error). It is not intended to catch exceptions raised in other plugins.
39 By default, <classname>Zend_Controller_Plugin_ErrorHandler</classname> will
40 forward to <methodname>ErrorController::errorAction()</methodname> in the default
41 module. You may set alternate values for these by using the various
42 accessors available to the plugin:
48 <methodname>setErrorHandlerModule()</methodname> sets the controller module to use.
54 <methodname>setErrorHandlerController()</methodname> sets the controller to use.
60 <methodname>setErrorHandlerAction()</methodname> sets the controller action to use.
66 <methodname>setErrorHandler()</methodname> takes an associative array, which
67 may contain any of the keys 'module', 'controller', or 'action',
68 with which it will set the appropriate values.
74 Additionally, you may pass an optional associative array to the
75 constructor, which will then proxy to <methodname>setErrorHandler()</methodname>.
79 <classname>Zend_Controller_Plugin_ErrorHandler</classname> registers a
80 <methodname>postDispatch()</methodname> hook and checks for exceptions registered in
81 <link linkend="zend.controller.response">the response object</link>. If
82 any are found, it attempts to forward to the registered error handler
87 If an exception occurs dispatching the error handler, the plugin will
88 tell the front controller to throw exceptions, and rethrow the last
89 exception registered with the response object.
92 <sect4 id="zend.controller.plugins.standard.errorhandler.fourohfour">
93 <title>Using the ErrorHandler as a 404 Handler</title>
96 Since the <emphasis>ErrorHandler</emphasis> plugin captures not only
97 application errors, but also errors in the controller chain arising
98 from missing controller classes and/or action methods, it can be
99 used as a 404 handler. To do so, you will need to have your error
100 controller check the exception type.
104 Exceptions captured are logged in an object registered in the
105 request. To retrieve it, use
106 <methodname>Zend_Controller_Action::_getParam('error_handler')</methodname>:
109 <programlisting language="php"><![CDATA[
110 class ErrorController extends Zend_Controller_Action
112 public function errorAction()
114 $errors = $this->_getParam('error_handler');
120 Once you have the error object, you can get the type via
121 <command>$errors->type;</command>. It will be one of the following:
127 <constant>Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE</constant>,
128 indicating no route matched.
134 <constant>Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER</constant>,
135 indicating the controller was not found.
141 <constant>Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION</constant>,
142 indicating the requested action was not found.
148 <constant>Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER</constant>,
149 indicating other exceptions.
155 You can then test for either of the first three types, and, if so,
159 <programlisting language="php"><![CDATA[
160 class ErrorController extends Zend_Controller_Action
162 public function errorAction()
164 $errors = $this->_getParam('error_handler');
166 switch ($errors->type) {
167 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
168 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
169 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
170 // 404 error -- controller or action not found
172 ->setRawHeader('HTTP/1.1 404 Not Found');
174 // ... get some output to display...
177 // application error; display error page, but don't
178 // change status code
186 Finally, you can retrieve the exception that triggered the error
187 handler by grabbing the <property>exception</property> property of the
188 <emphasis>error_handler</emphasis> object:
191 <programlisting language="php"><![CDATA[
192 public function errorAction()
194 $errors = $this->_getParam('error_handler');
196 switch ($errors->type) {
197 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
198 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
199 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
200 // 404 error -- controller or action not found
202 ->setRawHeader('HTTP/1.1 404 Not Found');
204 // ... get some output to display...
207 // application error; display error page, but don't change
212 // Log the exception:
213 $exception = $errors->exception;
215 new Zend_Log_Writer_Stream(
216 '/tmp/applicationException.log'
219 $log->debug($exception->getMessage() . "\n" .
220 $exception->getTraceAsString());
227 <sect4 id="zend.controller.plugins.standard.errorhandler.buffer">
228 <title>Handling Previously Rendered Output</title>
231 If you dispatch multiple actions in a request, or if your action
232 makes multiple calls to <methodname>render()</methodname>, it's possible that the
233 response object already has content stored within it. This can lead
234 to rendering a mixture of expected content and error content.
238 If you wish to render errors inline in such pages, no changes will
239 be necessary. If you do not wish to render such content, you should
240 clear the response body prior to rendering any views:
243 <programlisting language="php"><![CDATA[
244 $this->getResponse()->clearBody();
248 <sect4 id="zend.controller.plugins.standard.errorhandler.examples">
249 <title>Plugin Usage Examples</title>
251 <example id="zend.controller.plugins.standard.errorhandler.examples.example-1">
252 <title>Standard Usage</title>
254 <programlisting language="php"><![CDATA[
255 $front = Zend_Controller_Front::getInstance();
256 $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler());
260 <example id="zend.controller.plugins.standard.errorhandler.examples.example-2">
261 <title>Setting a Different Error Handler</title>
263 <programlisting language="php"><![CDATA[
264 $front = Zend_Controller_Front::getInstance();
265 $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(array(
266 'module' => 'mystuff',
267 'controller' => 'static',
273 <example id="zend.controller.plugins.standard.errorhandler.examples.example-3">
274 <title>Using Accessors</title>
276 <programlisting language="php"><![CDATA[
277 $plugin = new Zend_Controller_Plugin_ErrorHandler();
278 $plugin->setErrorHandlerModule('mystuff')
279 ->setErrorHandlerController('static')
280 ->setErrorHandlerAction('error');
282 $front = Zend_Controller_Front::getInstance();
283 $front->registerPlugin($plugin);
288 <sect4 id="zend.controller.plugins.standard.errorhandler.controllerexamples">
289 <title>Error Controller Example</title>
292 In order to use the Error Handler plugin, you need an error
293 controller. Below is a simple example.
296 <programlisting language="php"><![CDATA[
297 class ErrorController extends Zend_Controller_Action
299 public function errorAction()
301 $errors = $this->_getParam('error_handler');
303 switch ($errors->type) {
304 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
305 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
306 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
307 // 404 error -- controller or action not found
308 $this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
312 <p>The page you requested was not found.</p>
319 <p>An unexpected error occurred. Please try again later.</p>
324 // Clear previous content
325 $this->getResponse()->clearBody();
327 $this->view->content = $content;