PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Error/ErrorHandler.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 226 lines | 99 code | 6 blank | 121 comment | 7 complexity | e1afacc445ec25affa186ba7df7cb65c MD5 | raw file
  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-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Error
  18. * @since CakePHP(tm) v 0.10.5.1732
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Debugger', 'Utility');
  22. App::uses('CakeLog', 'Log');
  23. App::uses('ExceptionRenderer', 'Error');
  24. App::uses('AppController', 'Controller');
  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 supersede 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/Lib/Error.
  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.Error
  94. * @see ExceptionRenderer for more information on how to customize exception rendering.
  95. */
  96. class ErrorHandler {
  97. /**
  98. * Set as the default exception handler by the CakePHP bootstrap process.
  99. *
  100. * This will either use custom exception renderer class if configured,
  101. * or use the default ExceptionRenderer.
  102. *
  103. * @param Exception $exception
  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. $message = sprintf("[%s] %s\n%s",
  111. get_class($exception),
  112. $exception->getMessage(),
  113. $exception->getTraceAsString()
  114. );
  115. CakeLog::write(LOG_ERR, $message);
  116. }
  117. $renderer = $config['renderer'];
  118. if ($renderer !== 'ExceptionRenderer') {
  119. list($plugin, $renderer) = pluginSplit($renderer, true);
  120. App::uses($renderer, $plugin . 'Error');
  121. }
  122. try {
  123. $error = new $renderer($exception);
  124. $error->render();
  125. } catch (Exception $e) {
  126. set_error_handler(Configure::read('Error.handler')); // Should be using configured ErrorHandler
  127. Configure::write('Error.trace', false); // trace is useless here since it's internal
  128. $message = sprintf("[%s] %s\n%s", // Keeping same message format
  129. get_class($e),
  130. $e->getMessage(),
  131. $e->getTraceAsString()
  132. );
  133. trigger_error($message, E_USER_ERROR);
  134. }
  135. }
  136. /**
  137. * Set as the default error handler by CakePHP. Use Configure::write('Error.handler', $callback), to use your own
  138. * error handling methods. This function will use Debugger to display errors when debug > 0. And
  139. * will log errors to CakeLog, when debug == 0.
  140. *
  141. * You can use Configure::write('Error.level', $value); to set what type of errors will be handled here.
  142. * Stack traces for errors can be enabled with Configure::write('Error.trace', true);
  143. *
  144. * @param integer $code Code of error
  145. * @param string $description Error description
  146. * @param string $file File on which error occurred
  147. * @param integer $line Line that triggered the error
  148. * @param array $context Context
  149. * @return boolean true if error was handled
  150. */
  151. public static function handleError($code, $description, $file = null, $line = null, $context = null) {
  152. if (error_reporting() === 0) {
  153. return false;
  154. }
  155. $errorConfig = Configure::read('Error');
  156. list($error, $log) = self::mapErrorCode($code);
  157. $debug = Configure::read('debug');
  158. if ($debug) {
  159. $data = array(
  160. 'level' => $log,
  161. 'code' => $code,
  162. 'error' => $error,
  163. 'description' => $description,
  164. 'file' => $file,
  165. 'line' => $line,
  166. 'context' => $context,
  167. 'start' => 2,
  168. 'path' => Debugger::trimPath($file)
  169. );
  170. return Debugger::getInstance()->outputError($data);
  171. } else {
  172. $message = $error . ' (' . $code . '): ' . $description . ' in [' . $file . ', line ' . $line . ']';
  173. if (!empty($errorConfig['trace'])) {
  174. $trace = Debugger::trace(array('start' => 1, 'format' => 'log'));
  175. $message .= "\nTrace:\n" . $trace . "\n";
  176. }
  177. return CakeLog::write($log, $message);
  178. }
  179. }
  180. /**
  181. * Map an error code into an Error word, and log location.
  182. *
  183. * @param integer $code Error code to map
  184. * @return array Array of error word, and log location.
  185. */
  186. public static function mapErrorCode($code) {
  187. $error = $log = null;
  188. switch ($code) {
  189. case E_PARSE:
  190. case E_ERROR:
  191. case E_CORE_ERROR:
  192. case E_COMPILE_ERROR:
  193. case E_USER_ERROR:
  194. $error = 'Fatal Error';
  195. $log = LOG_ERROR;
  196. break;
  197. case E_WARNING:
  198. case E_USER_WARNING:
  199. case E_COMPILE_WARNING:
  200. case E_RECOVERABLE_ERROR:
  201. $error = 'Warning';
  202. $log = LOG_WARNING;
  203. break;
  204. case E_NOTICE:
  205. case E_USER_NOTICE:
  206. $error = 'Notice';
  207. $log = LOG_NOTICE;
  208. break;
  209. case E_STRICT:
  210. $error = 'Strict';
  211. $log = LOG_NOTICE;
  212. break;
  213. case E_DEPRECATED:
  214. $error = 'Deprecated';
  215. $log = LOG_NOTICE;
  216. break;
  217. }
  218. return array($error, $log);
  219. }
  220. }