ErrorHandler.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Error handler
  4. *
  5. * Provides Error Capturing for Framework errors.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.cake.libs
  19. * @since CakePHP(tm) v 0.10.5.1732
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('Debugger', 'Utility');
  23. /**
  24. *
  25. * Error Handler provides basic error and exception handling for your application. It captures and
  26. * handles all unhandled exceptions and errors. Displays helpful framework errors when debug > 1.
  27. *
  28. * ### Uncaught exceptions
  29. *
  30. * When debug < 1 a CakeException will render 404 or 500 errors. If an uncaught exception is thrown
  31. * and it is a type that ErrorHandler does not know about it will be treated as a 500 error.
  32. *
  33. * ### Implementing application specific exception handling
  34. *
  35. * You can implement application specific exception handling in one of a few ways. Each approach
  36. * gives you different amounts of control over the exception handling process.
  37. *
  38. * - Set Configure::write('Exception.handler', 'YourClass::yourMethod');
  39. * - Create AppController::appError();
  40. * - Set Configure::write('Exception.renderer', 'YourClass');
  41. *
  42. * #### Create your own Exception handler with `Exception.handler`
  43. *
  44. * This gives you full control over the exception handling process. The class you choose should be
  45. * loaded in your app/config/bootstrap.php, so its available to handle any exceptions. You can
  46. * define the handler as any callback type. Using Exception.handler overrides all other exception
  47. * handling settings and logic.
  48. *
  49. * #### Using `AppController::appError();`
  50. *
  51. * This controller method is called instead of the default exception rendering. It receives the
  52. * thrown exception as its only argument. You should implement your error handling in that method.
  53. * Using AppController::appError(), will superseed any configuration for Exception.renderer.
  54. *
  55. * #### Using a custom renderer with `Exception.renderer`
  56. *
  57. * If you don't want to take control of the exception handling, but want to change how exceptions are
  58. * rendered you can use `Exception.renderer` to choose a class to render exception pages. By default
  59. * `ExceptionRenderer` is used. Your custom exception renderer class should be placed in app/libs.
  60. *
  61. * Your custom renderer should expect an exception in its constructor, and implement a render method.
  62. * Failing to do so will cause additional errors.
  63. *
  64. * #### Logging exceptions
  65. *
  66. * Using the built-in exception handling, you can log all the exceptions
  67. * that are dealt with by ErrorHandler by setting `Exception.log` to true in your core.php.
  68. * Enabling this will log every exception to CakeLog and the configured loggers.
  69. *
  70. * ### PHP errors
  71. *
  72. * Error handler also provides the built in features for handling php errors (trigger_error).
  73. * While in debug mode, errors will be output to the screen using debugger. While in production mode,
  74. * errors will be logged to CakeLog. You can control which errors are logged by setting
  75. * `Error.level` in your core.php.
  76. *
  77. * #### Logging errors
  78. *
  79. * When ErrorHandler is used for handling errors, you can enable error logging by setting `Error.log` to true.
  80. * This will log all errors to the configured log handlers.
  81. *
  82. * #### Controlling what errors are logged/displayed
  83. *
  84. * You can control which errors are logged / displayed by ErrorHandler by setting `Error.level`. Setting this
  85. * to one or a combination of a few of the E_* constants will only enable the specified errors.
  86. *
  87. * e.g. `Configure::write('Error.level', E_ALL & ~E_NOTICE);`
  88. *
  89. * Would enable handling for all non Notice errors.
  90. *
  91. * @package cake
  92. * @subpackage cake.cake.libs
  93. * @see ExceptionRenderer for more information on how to customize exception rendering.
  94. */
  95. class ErrorHandler {
  96. /**
  97. * Set as the default exception handler by the CakePHP bootstrap process.
  98. *
  99. * This will either use an AppError class if your application has one,
  100. * or use the default ExceptionRenderer.
  101. *
  102. * @return void
  103. * @see http://php.net/manual/en/function.set-exception-handler.php
  104. */
  105. public static function handleException(Exception $exception) {
  106. App::import('Core', 'error/ExceptionRenderer');
  107. $config = Configure::read('Exception');
  108. if (!empty($config['log'])) {
  109. if (!class_exists('CakeLog')) {
  110. require LIBS . 'cake_log.php';
  111. }
  112. CakeLog::write(LOG_ERR, '[' . get_class($exception) . '] ' . $exception->getMessage());
  113. }
  114. if ($config['renderer'] !== 'ExceptionRenderer') {
  115. App::import('Lib', $config['renderer']);
  116. }
  117. $error = new $config['renderer']($exception);
  118. $error->render();
  119. }
  120. /**
  121. * Set as the default error handler by CakePHP. Use Configure::write('Error.handler', $callback), to use your own
  122. * error handling methods. This function will use Debugger to display errors when debug > 0. And
  123. * will log errors to CakeLog, when debug == 0.
  124. *
  125. * You can use Configure::write('Error.level', $value); to set what type of errors will be handled here.
  126. * Stack traces for errors can be enabled with Configure::write('Error.trace', true);
  127. *
  128. * @param integer $code Code of error
  129. * @param string $description Error description
  130. * @param string $file File on which error occurred
  131. * @param integer $line Line that triggered the error
  132. * @param array $context Context
  133. * @return boolean true if error was handled
  134. */
  135. public static function handleError($code, $description, $file = null, $line = null, $context = null) {
  136. if (error_reporting() === 0) {
  137. return false;
  138. }
  139. $errorConfig = Configure::read('Error');
  140. list($error, $log) = self::_mapErrorCode($code);
  141. $debug = Configure::read('debug');
  142. if ($debug) {
  143. $data = array(
  144. 'level' => $log,
  145. 'code' => $code,
  146. 'error' => $error,
  147. 'description' => $description,
  148. 'file' => $file,
  149. 'line' => $line,
  150. 'context' => $context,
  151. 'start' => 2,
  152. 'path' => Debugger::trimPath($file)
  153. );
  154. return Debugger::getInstance()->outputError($data);
  155. } else {
  156. if (!class_exists('CakeLog')) {
  157. require LIBS . 'cake_log.php';
  158. }
  159. $message = $error . ' (' . $code . '): ' . $description . ' in [' . $file . ', line ' . $line . ']';
  160. if (!empty($errorConfig['trace'])) {
  161. $trace = Debugger::trace(array('start' => 1, 'format' => 'log'));
  162. $message .= "\nTrace:\n" . $trace . "\n";
  163. }
  164. return CakeLog::write($log, $message);
  165. }
  166. }
  167. /**
  168. * Map an error code into an Error word, and log location.
  169. *
  170. * @param int $code Error code to map
  171. * @return array Array of error word, and log location.
  172. */
  173. protected static function _mapErrorCode($code) {
  174. switch ($code) {
  175. case E_PARSE:
  176. case E_ERROR:
  177. case E_CORE_ERROR:
  178. case E_COMPILE_ERROR:
  179. case E_USER_ERROR:
  180. $error = 'Fatal Error';
  181. $log = LOG_ERROR;
  182. break;
  183. case E_WARNING:
  184. case E_USER_WARNING:
  185. case E_COMPILE_WARNING:
  186. case E_RECOVERABLE_ERROR:
  187. $error = 'Warning';
  188. $log = LOG_WARNING;
  189. break;
  190. case E_NOTICE:
  191. case E_USER_NOTICE:
  192. $error = 'Notice';
  193. $log = LOG_NOTICE;
  194. break;
  195. case E_STRICT:
  196. $error = 'Strict';
  197. $log = LOG_NOTICE;
  198. break;
  199. case E_DEPRECATED:
  200. $error = 'Deprecated';
  201. $log = LOG_NOTICE;
  202. break;
  203. }
  204. return array($error, $log);
  205. }
  206. }