ExceptionRenderer.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP(tm) Project
  18. * @package cake.libs.error
  19. * @since CakePHP(tm) v 2.0
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('Sanitize', 'Utility');
  23. /**
  24. * Exception Renderer.
  25. *
  26. * Captures and handles all unhandled exceptions. Displays helpful framework errors when debug > 1.
  27. * When debug < 1 a CakeException will render 404 or 500 errors. If an uncaught exception is thrown
  28. * and it is a type that ExceptionHandler does not know about it will be treated as a 500 error.
  29. *
  30. * ### Implementing application specific exception rendering
  31. *
  32. * You can implement application specific exception handling in one of a few ways:
  33. *
  34. * - Create a AppController::appError();
  35. * - Create a subclass of ExceptionRenderer and configure it to be the `Exception.renderer`
  36. *
  37. * #### Using AppController::appError();
  38. *
  39. * This controller method is called instead of the default exception handling. It receives the
  40. * thrown exception as its only argument. You should implement your error handling in that method.
  41. *
  42. * #### Using a subclass of ExceptionRenderer
  43. *
  44. * Using a subclass of ExceptionRenderer gives you full control over how Exceptions are rendered, you
  45. * can configure your class in your core.php, with `Configure::write('Exception.renderer', 'MyClass');`
  46. * You should place any custom exception renderers in `app/libs`.
  47. *
  48. * @package cake.libs.error
  49. */
  50. class ExceptionRenderer {
  51. /**
  52. * Controller instance.
  53. *
  54. * @var Controller
  55. * @access public
  56. */
  57. public $controller = null;
  58. /**
  59. * template to render for CakeException
  60. *
  61. * @var string
  62. */
  63. public $template = '';
  64. /**
  65. * The method corresponding to the Exception this object is for.
  66. *
  67. * @var string
  68. */
  69. public $method = '';
  70. /**
  71. * The exception being handled.
  72. *
  73. * @var Exception
  74. */
  75. public $error = null;
  76. /**
  77. * Creates the controller to perform rendering on the error response.
  78. * If the error is a CakeException it will be converted to either a 400 or a 500
  79. * code error depending on the code used to construct the error.
  80. *
  81. * @param string $method Method producing the error
  82. * @param array $messages Error messages
  83. */
  84. function __construct(Exception $exception) {
  85. $this->controller = $this->_getController($exception);
  86. if (method_exists($this->controller, 'apperror')) {
  87. return $this->controller->appError($exception);
  88. }
  89. $method = $template = Inflector::variable(str_replace('Exception', '', get_class($exception)));
  90. $code = $exception->getCode();
  91. $methodExists = method_exists($this, $method);
  92. if ($exception instanceof CakeException && !$methodExists) {
  93. $method = '_cakeError';
  94. if (empty($template)) {
  95. $template = 'error500';
  96. }
  97. if ($template == 'internalError') {
  98. $template = 'error500';
  99. }
  100. } elseif (!$methodExists) {
  101. $method = 'error500';
  102. if ($code >= 400 && $code < 500) {
  103. $method = 'error400';
  104. }
  105. }
  106. if (Configure::read('debug') == 0) {
  107. $parentClass = get_parent_class($this);
  108. if ($parentClass != __CLASS__) {
  109. $method = 'error400';
  110. }
  111. $parentMethods = (array)get_class_methods($parentClass);
  112. if (in_array($method, $parentMethods)) {
  113. $method = 'error400';
  114. }
  115. if ($code == 500) {
  116. $method = 'error500';
  117. }
  118. }
  119. $this->template = $template;
  120. $this->method = $method;
  121. $this->error = $exception;
  122. }
  123. /**
  124. * Get the controller instance to handle the exception.
  125. * Override this method in subclasses to customize the controller used.
  126. * This method returns the built in `CakeErrorController` normally, or if an error is repeated
  127. * a bare controller will be used.
  128. *
  129. * @param Exception $exception The exception to get a controller for.
  130. * @return Controller
  131. */
  132. protected function _getController($exception) {
  133. static $__previousError = null;
  134. App::uses('CakeErrorController', 'Controller');
  135. if ($__previousError != $exception) {
  136. $__previousError = $exception;
  137. $controller = new CakeErrorController();
  138. } else {
  139. $controller = new Controller();
  140. $controller->viewPath = 'errors';
  141. }
  142. return $controller;
  143. }
  144. /**
  145. * Renders the response for the exception.
  146. *
  147. * @return void
  148. */
  149. public function render() {
  150. if ($this->method) {
  151. call_user_func_array(array($this, $this->method), array($this->error));
  152. }
  153. }
  154. /**
  155. * Generic handler for the internal framework errors CakePHP can generate.
  156. *
  157. * @param CakeExeption $error
  158. * @return void
  159. */
  160. protected function _cakeError(CakeException $error) {
  161. $url = Router::normalize($this->controller->request->here);
  162. $code = $error->getCode();
  163. $this->controller->response->statusCode($code);
  164. $this->controller->set(array(
  165. 'code' => $code,
  166. 'url' => h($url),
  167. 'name' => $error->getMessage(),
  168. 'error' => $error,
  169. ));
  170. $this->controller->set($error->getAttributes());
  171. $this->_outputMessage($this->template);
  172. }
  173. /**
  174. * Convenience method to display a 400 series page.
  175. *
  176. * @param array $params Parameters for controller
  177. */
  178. public function error400($error) {
  179. $message = $error->getMessage();
  180. if (Configure::read('debug') == 0 && $error instanceof CakeException) {
  181. $message = __('Not Found');
  182. }
  183. $url = Router::normalize($this->controller->request->here);
  184. $this->controller->response->statusCode($error->getCode());
  185. $this->controller->set(array(
  186. 'name' => $message,
  187. 'url' => h($url),
  188. 'error' => $error,
  189. ));
  190. $this->_outputMessage('error400');
  191. }
  192. /**
  193. * Convenience method to display a 500 page.
  194. *
  195. * @param array $params Parameters for controller
  196. */
  197. public function error500($error) {
  198. $url = Router::normalize($this->controller->request->here);
  199. $code = ($error->getCode() > 500) ? $error->getCode() : 500;
  200. $this->controller->response->statusCode($code);
  201. $this->controller->set(array(
  202. 'name' => __('An Internal Error Has Occurred'),
  203. 'message' => h($url),
  204. 'error' => $error,
  205. ));
  206. $this->_outputMessage('error500');
  207. }
  208. /**
  209. * Generate the response using the controller object.
  210. *
  211. * @param string $template The template to render.
  212. */
  213. protected function _outputMessage($template) {
  214. $this->controller->render($template);
  215. $this->controller->afterFilter();
  216. $this->controller->response->send();
  217. }
  218. }