ExceptionRenderer.php 9.3 KB

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