ExceptionRenderer.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * Exception Renderer
  4. *
  5. * Provides Exception rendering features. Which allow exceptions to be rendered
  6. * as HTML pages.
  7. *
  8. * PHP 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  11. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * For full copyright and license information, please see the LICENSE.txt
  15. * Redistributions of files must retain the above copyright notice.
  16. *
  17. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  18. * @link http://cakephp.org CakePHP(tm) Project
  19. * @package Cake.Error
  20. * @since CakePHP(tm) v 2.0
  21. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  22. */
  23. App::uses('Sanitize', 'Utility');
  24. App::uses('Router', 'Routing');
  25. App::uses('CakeResponse', 'Network');
  26. App::uses('Controller', 'Controller');
  27. /**
  28. * Exception Renderer.
  29. *
  30. * Captures and handles all unhandled exceptions. Displays helpful framework errors when debug > 1.
  31. * When debug < 1 a CakeException will render 404 or 500 errors. If an uncaught exception is thrown
  32. * and it is a type that ExceptionHandler does not know about it will be treated as a 500 error.
  33. *
  34. * ### Implementing application specific exception rendering
  35. *
  36. * You can implement application specific exception handling in one of a few ways:
  37. *
  38. * - Create a AppController::appError();
  39. * - Create a subclass of ExceptionRenderer and configure it to be the `Exception.renderer`
  40. *
  41. * #### Using AppController::appError();
  42. *
  43. * This controller method is called instead of the default exception handling. It receives the
  44. * thrown exception as its only argument. You should implement your error handling in that method.
  45. *
  46. * #### Using a subclass of ExceptionRenderer
  47. *
  48. * Using a subclass of ExceptionRenderer gives you full control over how Exceptions are rendered, you
  49. * can configure your class in your core.php, with `Configure::write('Exception.renderer', 'MyClass');`
  50. * You should place any custom exception renderers in `app/Lib/Error`.
  51. *
  52. * @package Cake.Error
  53. */
  54. class ExceptionRenderer {
  55. /**
  56. * Controller instance.
  57. *
  58. * @var Controller
  59. */
  60. public $controller = null;
  61. /**
  62. * template to render for CakeException
  63. *
  64. * @var string
  65. */
  66. public $template = '';
  67. /**
  68. * The method corresponding to the Exception this object is for.
  69. *
  70. * @var string
  71. */
  72. public $method = '';
  73. /**
  74. * The exception being handled.
  75. *
  76. * @var Exception
  77. */
  78. public $error = null;
  79. /**
  80. * Creates the controller to perform rendering on the error response.
  81. * If the error is a CakeException it will be converted to either a 400 or a 500
  82. * code error depending on the code used to construct the error.
  83. *
  84. * @param Exception $exception Exception
  85. */
  86. public function __construct(Exception $exception) {
  87. $this->controller = $this->_getController($exception);
  88. if (method_exists($this->controller, 'apperror')) {
  89. return $this->controller->appError($exception);
  90. }
  91. $method = $template = Inflector::variable(str_replace('Exception', '', get_class($exception)));
  92. $code = $exception->getCode();
  93. $methodExists = method_exists($this, $method);
  94. if ($exception instanceof CakeException && !$methodExists) {
  95. $method = '_cakeError';
  96. if (empty($template) || $template === 'internalError') {
  97. $template = 'error500';
  98. }
  99. } elseif ($exception instanceof PDOException) {
  100. $method = 'pdoError';
  101. $template = 'pdo_error';
  102. $code = 500;
  103. } elseif (!$methodExists) {
  104. $method = 'error500';
  105. if ($code >= 400 && $code < 500) {
  106. $method = 'error400';
  107. }
  108. }
  109. $isNotDebug = !Configure::read('debug');
  110. if ($isNotDebug && $method === '_cakeError') {
  111. $method = 'error400';
  112. }
  113. if ($isNotDebug && $code == 500) {
  114. $method = 'error500';
  115. }
  116. $this->template = $template;
  117. $this->method = $method;
  118. $this->error = $exception;
  119. }
  120. /**
  121. * Get the controller instance to handle the exception.
  122. * Override this method in subclasses to customize the controller used.
  123. * This method returns the built in `CakeErrorController` normally, or if an error is repeated
  124. * a bare controller will be used.
  125. *
  126. * @param Exception $exception The exception to get a controller for.
  127. * @return Controller
  128. */
  129. protected function _getController($exception) {
  130. App::uses('AppController', 'Controller');
  131. App::uses('CakeErrorController', 'Controller');
  132. if (!$request = Router::getRequest(true)) {
  133. $request = new CakeRequest();
  134. }
  135. $response = new CakeResponse();
  136. if (method_exists($exception, 'responseHeader')) {
  137. $response->header($exception->responseHeader());
  138. }
  139. if (class_exists('AppController')) {
  140. try {
  141. $controller = new CakeErrorController($request, $response);
  142. $controller->startupProcess();
  143. } catch (Exception $e) {
  144. if (!empty($controller) && $controller->Components->enabled('RequestHandler')) {
  145. $controller->RequestHandler->startup($controller);
  146. }
  147. }
  148. }
  149. if (empty($controller)) {
  150. $controller = new Controller($request, $response);
  151. $controller->viewPath = 'Errors';
  152. }
  153. return $controller;
  154. }
  155. /**
  156. * Renders the response for the exception.
  157. *
  158. * @return void
  159. */
  160. public function render() {
  161. if ($this->method) {
  162. call_user_func_array(array($this, $this->method), array($this->error));
  163. }
  164. }
  165. /**
  166. * Generic handler for the internal framework errors CakePHP can generate.
  167. *
  168. * @param CakeException $error
  169. * @return void
  170. */
  171. protected function _cakeError(CakeException $error) {
  172. $url = $this->controller->request->here();
  173. $code = ($error->getCode() >= 400 && $error->getCode() < 506) ? $error->getCode() : 500;
  174. $this->controller->response->statusCode($code);
  175. $this->controller->set(array(
  176. 'code' => $code,
  177. 'url' => h($url),
  178. 'name' => h($error->getMessage()),
  179. 'error' => $error,
  180. '_serialize' => array('code', 'url', 'name')
  181. ));
  182. $this->controller->set($error->getAttributes());
  183. $this->_outputMessage($this->template);
  184. }
  185. /**
  186. * Convenience method to display a 400 series page.
  187. *
  188. * @param Exception $error
  189. * @return void
  190. */
  191. public function error400($error) {
  192. $message = $error->getMessage();
  193. if (!Configure::read('debug') && $error instanceof CakeException) {
  194. $message = __d('cake', 'Not Found');
  195. }
  196. $url = $this->controller->request->here();
  197. $this->controller->response->statusCode($error->getCode());
  198. $this->controller->set(array(
  199. 'name' => h($message),
  200. 'url' => h($url),
  201. 'error' => $error,
  202. '_serialize' => array('name', 'url')
  203. ));
  204. $this->_outputMessage('error400');
  205. }
  206. /**
  207. * Convenience method to display a 500 page.
  208. *
  209. * @param Exception $error
  210. * @return void
  211. */
  212. public function error500($error) {
  213. $message = $error->getMessage();
  214. if (!Configure::read('debug')) {
  215. $message = __d('cake', 'An Internal Error Has Occurred.');
  216. }
  217. $url = $this->controller->request->here();
  218. $code = ($error->getCode() > 500 && $error->getCode() < 506) ? $error->getCode() : 500;
  219. $this->controller->response->statusCode($code);
  220. $this->controller->set(array(
  221. 'name' => h($message),
  222. 'message' => h($url),
  223. 'error' => $error,
  224. '_serialize' => array('name', 'message')
  225. ));
  226. $this->_outputMessage('error500');
  227. }
  228. /**
  229. * Convenience method to display a PDOException.
  230. *
  231. * @param PDOException $error
  232. * @return void
  233. */
  234. public function pdoError(PDOException $error) {
  235. $url = $this->controller->request->here();
  236. $code = 500;
  237. $this->controller->response->statusCode($code);
  238. $this->controller->set(array(
  239. 'code' => $code,
  240. 'url' => h($url),
  241. 'name' => h($error->getMessage()),
  242. 'error' => $error,
  243. '_serialize' => array('code', 'url', 'name', 'error')
  244. ));
  245. $this->_outputMessage($this->template);
  246. }
  247. /**
  248. * Generate the response using the controller object.
  249. *
  250. * @param string $template The template to render.
  251. * @return void
  252. */
  253. protected function _outputMessage($template) {
  254. try {
  255. $this->controller->render($template);
  256. $this->controller->afterFilter();
  257. $this->controller->response->send();
  258. } catch (MissingViewException $e) {
  259. $attributes = $e->getAttributes();
  260. if (isset($attributes['file']) && strpos($attributes['file'], 'error500') !== false) {
  261. $this->_outputMessageSafe('error500');
  262. } else {
  263. $this->_outputMessage('error500');
  264. }
  265. } catch (Exception $e) {
  266. $this->_outputMessageSafe('error500');
  267. }
  268. }
  269. /**
  270. * A safer way to render error messages, replaces all helpers, with basics
  271. * and doesn't call component methods.
  272. *
  273. * @param string $template The template to render
  274. * @return void
  275. */
  276. protected function _outputMessageSafe($template) {
  277. $this->controller->layoutPath = null;
  278. $this->controller->subDir = null;
  279. $this->controller->viewPath = 'Errors';
  280. $this->controller->layout = 'error';
  281. $this->controller->helpers = array('Form', 'Html', 'Session');
  282. $view = new View($this->controller);
  283. $this->controller->response->body($view->render($template, 'error'));
  284. $this->controller->response->type('html');
  285. $this->controller->response->send();
  286. }
  287. }