PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/cakephp/cakephp/src/Error/ErrorHandler.php

https://gitlab.com/alexandresgv/siteentec
PHP | 188 lines | 60 code | 8 blank | 120 comment | 4 complexity | dec7ae30cbaf363744c2c232cd612eb1 MD5 | raw file
  1. <?php
  2. /**
  3. * ErrorHandler class
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since 0.10.5
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Error;
  18. use Cake\Core\App;
  19. use Exception;
  20. /**
  21. * Error Handler provides basic error and exception handling for your application. It captures and
  22. * handles all unhandled exceptions and errors. Displays helpful framework errors when debug > 1.
  23. *
  24. * ### Uncaught exceptions
  25. *
  26. * When debug < 1 a CakeException will render 404 or 500 errors. If an uncaught exception is thrown
  27. * and it is a type that ErrorHandler does not know about it will be treated as a 500 error.
  28. *
  29. * ### Implementing application specific exception handling
  30. *
  31. * You can implement application specific exception handling in one of a few ways. Each approach
  32. * gives you different amounts of control over the exception handling process.
  33. *
  34. * - Modify config/error.php and setup custom exception handling.
  35. * - Use the `exceptionRenderer` option to inject an Exception renderer. This will
  36. * let you keep the existing handling logic but override the rendering logic.
  37. *
  38. * #### Create your own Exception handler
  39. *
  40. * This gives you full control over the exception handling process. The class you choose should be
  41. * loaded in your config/error.php and registered as the default exception handler.
  42. *
  43. * #### Using a custom renderer with `exceptionRenderer`
  44. *
  45. * If you don't want to take control of the exception handling, but want to change how exceptions are
  46. * rendered you can use `exceptionRenderer` option to choose a class to render exception pages. By default
  47. * `Cake\Error\ExceptionRenderer` is used. Your custom exception renderer class should be placed in src/Error.
  48. *
  49. * Your custom renderer should expect an exception in its constructor, and implement a render method.
  50. * Failing to do so will cause additional errors.
  51. *
  52. * #### Logging exceptions
  53. *
  54. * Using the built-in exception handling, you can log all the exceptions
  55. * that are dealt with by ErrorHandler by setting `log` option to true in your config/error.php.
  56. * Enabling this will log every exception to Log and the configured loggers.
  57. *
  58. * ### PHP errors
  59. *
  60. * Error handler also provides the built in features for handling php errors (trigger_error).
  61. * While in debug mode, errors will be output to the screen using debugger. While in production mode,
  62. * errors will be logged to Log. You can control which errors are logged by setting
  63. * `errorLevel` option in config/error.php.
  64. *
  65. * #### Logging errors
  66. *
  67. * When ErrorHandler is used for handling errors, you can enable error logging by setting the `log`
  68. * option to true. This will log all errors to the configured log handlers.
  69. *
  70. * #### Controlling what errors are logged/displayed
  71. *
  72. * You can control which errors are logged / displayed by ErrorHandler by setting `errorLevel`. Setting this
  73. * to one or a combination of a few of the E_* constants will only enable the specified errors:
  74. *
  75. * ```
  76. * $options['errorLevel'] = E_ALL & ~E_NOTICE;
  77. * ```
  78. *
  79. * Would enable handling for all non Notice errors.
  80. *
  81. * @see ExceptionRenderer for more information on how to customize exception rendering.
  82. */
  83. class ErrorHandler extends BaseErrorHandler
  84. {
  85. /**
  86. * Options to use for the Error handling.
  87. *
  88. * @var array
  89. */
  90. protected $_options = [];
  91. /**
  92. * Constructor
  93. *
  94. * @param array $options The options for error handling.
  95. */
  96. public function __construct($options = [])
  97. {
  98. $defaults = [
  99. 'log' => true,
  100. 'trace' => false,
  101. 'exceptionRenderer' => 'Cake\Error\ExceptionRenderer',
  102. ];
  103. $this->_options = $options + $defaults;
  104. }
  105. /**
  106. * Display an error.
  107. *
  108. * Template method of BaseErrorHandler.
  109. *
  110. * Only when debug > 2 will a formatted error be displayed.
  111. *
  112. * @param array $error An array of error data.
  113. * @param bool $debug Whether or not the app is in debug mode.
  114. * @return void
  115. */
  116. protected function _displayError($error, $debug)
  117. {
  118. if (!$debug) {
  119. return;
  120. }
  121. Debugger::getInstance()->outputError($error);
  122. }
  123. /**
  124. * Displays an exception response body.
  125. *
  126. * @param \Exception $exception The exception to display
  127. * @return void
  128. * @throws \Exception When the chosen exception renderer is invalid.
  129. */
  130. protected function _displayException($exception)
  131. {
  132. $renderer = App::className($this->_options['exceptionRenderer'], 'Error');
  133. try {
  134. if (!$renderer) {
  135. throw new Exception("$renderer is an invalid class.");
  136. }
  137. $error = new $renderer($exception);
  138. $response = $error->render();
  139. $this->_clearOutput();
  140. $this->_sendResponse($response);
  141. } catch (Exception $e) {
  142. // Disable trace for internal errors.
  143. $this->_options['trace'] = false;
  144. $message = sprintf(
  145. "[%s] %s\n%s", // Keeping same message format
  146. get_class($e),
  147. $e->getMessage(),
  148. $e->getTraceAsString()
  149. );
  150. trigger_error($message, E_USER_ERROR);
  151. }
  152. }
  153. /**
  154. * Clear output buffers so error pages display properly.
  155. *
  156. * Easily stubbed in testing.
  157. *
  158. * @return void
  159. */
  160. protected function _clearOutput()
  161. {
  162. while (ob_get_level()) {
  163. ob_end_clean();
  164. }
  165. }
  166. /**
  167. * Method that can be easily stubbed in testing.
  168. *
  169. * @param string|\Cake\Network\Response $response Either the message or response object.
  170. * @return void
  171. */
  172. protected function _sendResponse($response)
  173. {
  174. if (is_string($response)) {
  175. echo $response;
  176. return;
  177. }
  178. $response->send();
  179. }
  180. }