ErrorHandler.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage Plugins
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Controller_Plugin_Abstract */
  22. require_once 'Zend/Controller/Plugin/Abstract.php';
  23. /**
  24. * Handle exceptions that bubble up based on missing controllers, actions, or
  25. * application errors, and forward to an error handler.
  26. *
  27. * @uses Zend_Controller_Plugin_Abstract
  28. * @category Zend
  29. * @package Zend_Controller
  30. * @subpackage Plugins
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @version $Id$
  34. */
  35. class Zend_Controller_Plugin_ErrorHandler extends Zend_Controller_Plugin_Abstract
  36. {
  37. /**
  38. * Const - No controller exception; controller does not exist
  39. */
  40. const EXCEPTION_NO_CONTROLLER = 'EXCEPTION_NO_CONTROLLER';
  41. /**
  42. * Const - No action exception; controller exists, but action does not
  43. */
  44. const EXCEPTION_NO_ACTION = 'EXCEPTION_NO_ACTION';
  45. /**
  46. * Const - No route exception; no routing was possible
  47. */
  48. const EXCEPTION_NO_ROUTE = 'EXCEPTION_NO_ROUTE';
  49. /**
  50. * Const - Other Exception; exceptions thrown by application controllers
  51. */
  52. const EXCEPTION_OTHER = 'EXCEPTION_OTHER';
  53. /**
  54. * Module to use for errors; defaults to default module in dispatcher
  55. * @var string
  56. */
  57. protected $_errorModule;
  58. /**
  59. * Controller to use for errors; defaults to 'error'
  60. * @var string
  61. */
  62. protected $_errorController = 'error';
  63. /**
  64. * Action to use for errors; defaults to 'error'
  65. * @var string
  66. */
  67. protected $_errorAction = 'error';
  68. /**
  69. * Flag; are we already inside the error handler loop?
  70. * @var bool
  71. */
  72. protected $_isInsideErrorHandlerLoop = false;
  73. /**
  74. * Exception count logged at first invocation of plugin
  75. * @var int
  76. */
  77. protected $_exceptionCountAtFirstEncounter = 0;
  78. /**
  79. * Constructor
  80. *
  81. * Options may include:
  82. * - module
  83. * - controller
  84. * - action
  85. *
  86. * @param Array $options
  87. * @return void
  88. */
  89. public function __construct(Array $options = array())
  90. {
  91. $this->setErrorHandler($options);
  92. }
  93. /**
  94. * setErrorHandler() - setup the error handling options
  95. *
  96. * @param array $options
  97. * @return Zend_Controller_Plugin_ErrorHandler
  98. */
  99. public function setErrorHandler(Array $options = array())
  100. {
  101. if (isset($options['module'])) {
  102. $this->setErrorHandlerModule($options['module']);
  103. }
  104. if (isset($options['controller'])) {
  105. $this->setErrorHandlerController($options['controller']);
  106. }
  107. if (isset($options['action'])) {
  108. $this->setErrorHandlerAction($options['action']);
  109. }
  110. return $this;
  111. }
  112. /**
  113. * Set the module name for the error handler
  114. *
  115. * @param string $module
  116. * @return Zend_Controller_Plugin_ErrorHandler
  117. */
  118. public function setErrorHandlerModule($module)
  119. {
  120. $this->_errorModule = (string) $module;
  121. return $this;
  122. }
  123. /**
  124. * Retrieve the current error handler module
  125. *
  126. * @return string
  127. */
  128. public function getErrorHandlerModule()
  129. {
  130. if (null === $this->_errorModule) {
  131. $this->_errorModule = Zend_Controller_Front::getInstance()->getDispatcher()->getDefaultModule();
  132. }
  133. return $this->_errorModule;
  134. }
  135. /**
  136. * Set the controller name for the error handler
  137. *
  138. * @param string $controller
  139. * @return Zend_Controller_Plugin_ErrorHandler
  140. */
  141. public function setErrorHandlerController($controller)
  142. {
  143. $this->_errorController = (string) $controller;
  144. return $this;
  145. }
  146. /**
  147. * Retrieve the current error handler controller
  148. *
  149. * @return string
  150. */
  151. public function getErrorHandlerController()
  152. {
  153. return $this->_errorController;
  154. }
  155. /**
  156. * Set the action name for the error handler
  157. *
  158. * @param string $action
  159. * @return Zend_Controller_Plugin_ErrorHandler
  160. */
  161. public function setErrorHandlerAction($action)
  162. {
  163. $this->_errorAction = (string) $action;
  164. return $this;
  165. }
  166. /**
  167. * Retrieve the current error handler action
  168. *
  169. * @return string
  170. */
  171. public function getErrorHandlerAction()
  172. {
  173. return $this->_errorAction;
  174. }
  175. /**
  176. * Route shutdown hook -- Ccheck for router exceptions
  177. *
  178. * @param Zend_Controller_Request_Abstract $request
  179. */
  180. public function routeShutdown(Zend_Controller_Request_Abstract $request)
  181. {
  182. $this->_handleError($request);
  183. }
  184. /**
  185. * Post dispatch hook -- check for exceptions and dispatch error handler if
  186. * necessary
  187. *
  188. * @param Zend_Controller_Request_Abstract $request
  189. */
  190. public function postDispatch(Zend_Controller_Request_Abstract $request)
  191. {
  192. $this->_handleError($request);
  193. }
  194. /**
  195. * Handle errors and exceptions
  196. *
  197. * If the 'noErrorHandler' front controller flag has been set,
  198. * returns early.
  199. *
  200. * @param Zend_Controller_Request_Abstract $request
  201. * @return void
  202. */
  203. protected function _handleError(Zend_Controller_Request_Abstract $request)
  204. {
  205. $frontController = Zend_Controller_Front::getInstance();
  206. if ($frontController->getParam('noErrorHandler')) {
  207. return;
  208. }
  209. $response = $this->getResponse();
  210. if ($this->_isInsideErrorHandlerLoop) {
  211. $exceptions = $response->getException();
  212. if (count($exceptions) > $this->_exceptionCountAtFirstEncounter) {
  213. // Exception thrown by error handler; tell the front controller to throw it
  214. $frontController->throwExceptions(true);
  215. throw array_pop($exceptions);
  216. }
  217. }
  218. // check for an exception AND allow the error handler controller the option to forward
  219. if (($response->isException()) && (!$this->_isInsideErrorHandlerLoop)) {
  220. $this->_isInsideErrorHandlerLoop = true;
  221. // Get exception information
  222. $error = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  223. $exceptions = $response->getException();
  224. $exception = $exceptions[0];
  225. $exceptionType = get_class($exception);
  226. $error->exception = $exception;
  227. switch ($exceptionType) {
  228. case 'Zend_Controller_Router_Exception':
  229. if (404 == $exception->getCode()) {
  230. $error->type = self::EXCEPTION_NO_ROUTE;
  231. } else {
  232. $error->type = self::EXCEPTION_OTHER;
  233. }
  234. break;
  235. case 'Zend_Controller_Dispatcher_Exception':
  236. $error->type = self::EXCEPTION_NO_CONTROLLER;
  237. break;
  238. case 'Zend_Controller_Action_Exception':
  239. if (404 == $exception->getCode()) {
  240. $error->type = self::EXCEPTION_NO_ACTION;
  241. } else {
  242. $error->type = self::EXCEPTION_OTHER;
  243. }
  244. break;
  245. default:
  246. $error->type = self::EXCEPTION_OTHER;
  247. break;
  248. }
  249. // Keep a copy of the original request
  250. $error->request = clone $request;
  251. // get a count of the number of exceptions encountered
  252. $this->_exceptionCountAtFirstEncounter = count($exceptions);
  253. // Forward to the error handler
  254. $request->setParam('error_handler', $error)
  255. ->setModuleName($this->getErrorHandlerModule())
  256. ->setControllerName($this->getErrorHandlerController())
  257. ->setActionName($this->getErrorHandlerAction())
  258. ->setDispatched(false);
  259. }
  260. }
  261. }