Zend_Controller-Plugins-ErrorHandler.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.controller.plugins.standard.errorhandler">
  4. <title>Zend_Controller_Plugin_ErrorHandler</title>
  5. <para>
  6. <classname>Zend_Controller_Plugin_ErrorHandler</classname> provides a drop-in
  7. plugin for handling exceptions thrown by your application, including
  8. those resulting from missing controllers or actions; it is an
  9. alternative to the methods listed in the <link
  10. linkend="zend.controller.exceptions">MVC Exceptions section</link>.
  11. </para>
  12. <para>
  13. The primary targets of the plugin are:
  14. </para>
  15. <itemizedlist>
  16. <listitem>
  17. <para>Intercept exceptions raised due to missing controllers or action methods</para>
  18. </listitem>
  19. <listitem>
  20. <para>Intercept exceptions raised within action controllers</para>
  21. </listitem>
  22. </itemizedlist>
  23. <para>
  24. In other words, the <emphasis>ErrorHandler</emphasis> plugin is designed to
  25. handle <acronym>HTTP</acronym> 404-type errors (page missing) and 500-type errors (internal
  26. error). It is not intended to catch exceptions raised in other plugins
  27. or routing.
  28. </para>
  29. <para>
  30. By default, <classname>Zend_Controller_Plugin_ErrorHandler</classname> will
  31. forward to <methodname>ErrorController::errorAction()</methodname> in the default
  32. module. You may set alternate values for these by using the various
  33. accessors available to the plugin:
  34. </para>
  35. <itemizedlist>
  36. <listitem>
  37. <para>
  38. <methodname>setErrorHandlerModule()</methodname> sets the controller module
  39. to use.
  40. </para>
  41. </listitem>
  42. <listitem>
  43. <para>
  44. <methodname>setErrorHandlerController()</methodname> sets the controller
  45. to use.
  46. </para>
  47. </listitem>
  48. <listitem>
  49. <para>
  50. <methodname>setErrorHandlerAction()</methodname> sets the controller action
  51. to use.
  52. </para>
  53. </listitem>
  54. <listitem>
  55. <para>
  56. <methodname>setErrorHandler()</methodname> takes an associative array, which
  57. may contain any of the keys 'module', 'controller', or 'action',
  58. with which it will set the appropriate values.
  59. </para>
  60. </listitem>
  61. </itemizedlist>
  62. <para>
  63. Additionally, you may pass an optional associative array to the
  64. constructor, which will then proxy to <methodname>setErrorHandler()</methodname>.
  65. </para>
  66. <para>
  67. <classname>Zend_Controller_Plugin_ErrorHandler</classname> registers a
  68. <methodname>postDispatch()</methodname> hook and checks for exceptions registered in
  69. <link linkend="zend.controller.response">the response object</link>. If
  70. any are found, it attempts to forward to the registered error handler
  71. action.
  72. </para>
  73. <para>
  74. If an exception occurs dispatching the error handler, the plugin will
  75. tell the front controller to throw exceptions, and rethrow the last
  76. exception registered with the response object.
  77. </para>
  78. <sect4 id="zend.controller.plugins.standard.errorhandler.fourohfour">
  79. <title>Using the ErrorHandler as a 404 Handler</title>
  80. <para>
  81. Since the <emphasis>ErrorHandler</emphasis> plugin captures not only
  82. application errors, but also errors in the controller chain arising
  83. from missing controller classes and/or action methods, it can be
  84. used as a 404 handler. To do so, you will need to have your error
  85. controller check the exception type.
  86. </para>
  87. <para>
  88. Exceptions captured are logged in an object registered in the
  89. request. To retrieve it, use
  90. <methodname>Zend_Controller_Action::_getParam('error_handler')</methodname>:
  91. </para>
  92. <programlisting language="php"><![CDATA[
  93. class ErrorController extends Zend_Controller_Action
  94. {
  95. public function errorAction()
  96. {
  97. $errors = $this->_getParam('error_handler');
  98. }
  99. }
  100. ]]></programlisting>
  101. <para>
  102. Once you have the error object, you can get the type via
  103. <command>$errors->type;</command>. It will be one of the following:
  104. </para>
  105. <itemizedlist>
  106. <listitem>
  107. <para>
  108. <constant>Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER</constant>,
  109. indicating the controller was not found.
  110. </para>
  111. </listitem>
  112. <listitem>
  113. <para>
  114. <constant>Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION</constant>,
  115. indicating the requested action was not found.
  116. </para>
  117. </listitem>
  118. <listitem>
  119. <para>
  120. <constant>Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER</constant>,
  121. indicating other exceptions.
  122. </para>
  123. </listitem>
  124. </itemizedlist>
  125. <para>
  126. You can then test for either of the first two types, and, if so,
  127. indicate a 404 page:
  128. </para>
  129. <programlisting language="php"><![CDATA[
  130. class ErrorController extends Zend_Controller_Action
  131. {
  132. public function errorAction()
  133. {
  134. $errors = $this->_getParam('error_handler');
  135. switch ($errors->type) {
  136. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  137. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  138. // 404 error -- controller or action not found
  139. $this->getResponse()
  140. ->setRawHeader('HTTP/1.1 404 Not Found');
  141. // ... get some output to display...
  142. break;
  143. default:
  144. // application error; display error page, but don't
  145. // change status code
  146. break;
  147. }
  148. }
  149. }
  150. ]]></programlisting>
  151. <para>
  152. Finally, you can retrieve the exception that triggered the error
  153. handler by grabbing the <property>exception</property> property of the
  154. <emphasis>error_handler</emphasis> object:
  155. </para>
  156. <programlisting language="php"><![CDATA[
  157. public function errorAction()
  158. {
  159. $errors = $this->_getParam('error_handler');
  160. switch ($errors->type) {
  161. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  162. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  163. // 404 error -- controller or action not found
  164. $this->getResponse()
  165. ->setRawHeader('HTTP/1.1 404 Not Found');
  166. // ... get some output to display...
  167. break;
  168. default:
  169. // application error; display error page, but don't change
  170. // status code
  171. // ...
  172. // Log the exception:
  173. $exception = $errors->exception;
  174. $log = new Zend_Log(
  175. new Zend_Log_Writer_Stream(
  176. '/tmp/applicationException.log'
  177. )
  178. );
  179. $log->debug($exception->getMessage() . "\n" .
  180. $exception->getTraceAsString());
  181. break;
  182. }
  183. }
  184. ]]></programlisting>
  185. </sect4>
  186. <sect4 id="zend.controller.plugins.standard.errorhandler.buffer">
  187. <title>Handling Previously Rendered Output</title>
  188. <para>
  189. If you dispatch multiple actions in a request, or if your action
  190. makes multiple calls to <methodname>render()</methodname>, it's possible that the
  191. response object already has content stored within it. This can lead
  192. to rendering a mixture of expected content and error content.
  193. </para>
  194. <para>
  195. If you wish to render errors inline in such pages, no changes will
  196. be necessary. If you do not wish to render such content, you should
  197. clear the response body prior to rendering any views:
  198. </para>
  199. <programlisting language="php"><![CDATA[
  200. $this->getResponse()->clearBody();
  201. ]]></programlisting>
  202. </sect4>
  203. <sect4 id="zend.controller.plugins.standard.errorhandler.examples">
  204. <title>Plugin Usage Examples</title>
  205. <example id="zend.controller.plugins.standard.errorhandler.examples.example-1">
  206. <title>Standard Usage</title>
  207. <programlisting language="php"><![CDATA[
  208. $front = Zend_Controller_Front::getInstance();
  209. $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler());
  210. ]]></programlisting>
  211. </example>
  212. <example id="zend.controller.plugins.standard.errorhandler.examples.example-2">
  213. <title>Setting a Different Error Handler</title>
  214. <programlisting language="php"><![CDATA[
  215. $front = Zend_Controller_Front::getInstance();
  216. $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(array(
  217. 'module' => 'mystuff',
  218. 'controller' => 'static',
  219. 'action' => 'error'
  220. )));
  221. ]]></programlisting>
  222. </example>
  223. <example id="zend.controller.plugins.standard.errorhandler.examples.example-3">
  224. <title>Using Accessors</title>
  225. <programlisting language="php"><![CDATA[
  226. $plugin = new Zend_Controller_Plugin_ErrorHandler();
  227. $plugin->setErrorHandlerModule('mystuff')
  228. ->setErrorHandlerController('static')
  229. ->setErrorHandlerAction('error');
  230. $front = Zend_Controller_Front::getInstance();
  231. $front->registerPlugin($plugin);
  232. ]]></programlisting>
  233. </example>
  234. </sect4>
  235. <sect4 id="zend.controller.plugins.standard.errorhandler.controllerexamples">
  236. <title>Error Controller Example</title>
  237. <para>
  238. In order to use the Error Handler plugin, you need an error
  239. controller. Below is a simple example.
  240. </para>
  241. <programlisting language="php"><![CDATA[
  242. class ErrorController extends Zend_Controller_Action
  243. {
  244. public function errorAction()
  245. {
  246. $errors = $this->_getParam('error_handler');
  247. switch ($errors->type) {
  248. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  249. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  250. // 404 error -- controller or action not found
  251. $this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
  252. $content =<<<EOH
  253. <h1>Error!</h1>
  254. <p>The page you requested was not found.</p>
  255. EOH;
  256. break;
  257. default:
  258. // application error
  259. $content =<<<EOH
  260. <h1>Error!</h1>
  261. <p>An unexpected error occurred. Please try again later.</p>
  262. EOH;
  263. break;
  264. }
  265. // Clear previous content
  266. $this->getResponse()->clearBody();
  267. $this->view->content = $content;
  268. }
  269. }
  270. ]]></programlisting>
  271. </sect4>
  272. </sect3>
  273. <!--
  274. vim:se ts=4 sw=4 et:
  275. -->