ErrorHandler.php 7.3 KB

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