ErrorException.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://www.yiiframework.com/license/
  6. */
  7. namespace yii\base;
  8. /**
  9. * ErrorException represents a PHP error.
  10. *
  11. * For more details and usage information on ErrorException, see the [guide article on handling errors](guide:runtime-handling-errors).
  12. *
  13. * @author Alexander Makarov <sam@rmcreative.ru>
  14. * @since 2.0
  15. */
  16. class ErrorException extends \ErrorException
  17. {
  18. /**
  19. * This constant represents a fatal error in the HHVM engine.
  20. *
  21. * PHP Zend runtime won't call the error handler on fatals, HHVM will, with an error code of 16777217
  22. * We will handle fatal error a bit different on HHVM.
  23. * @see https://github.com/facebook/hhvm/blob/master/hphp/runtime/base/runtime-error.h#L62
  24. * @since 2.0.6
  25. */
  26. const E_HHVM_FATAL_ERROR = 16777217; // E_ERROR | (1 << 24)
  27. /**
  28. * Constructs the exception.
  29. * @link https://www.php.net/manual/en/errorexception.construct.php
  30. * @param string $message [optional]
  31. * @param int $code [optional]
  32. * @param int $severity [optional]
  33. * @param string $filename [optional]
  34. * @param int $lineno [optional]
  35. * @param \Throwable|null $previous [optional]
  36. */
  37. public function __construct($message = '', $code = 0, $severity = 1, $filename = __FILE__, $lineno = __LINE__, $previous = null)
  38. {
  39. parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
  40. if ($this->isXdebugStackAvailable()) {
  41. // Xdebug trace can't be modified and used directly with PHP 7
  42. // @see https://github.com/yiisoft/yii2/pull/11723
  43. $xdebugTrace = array_slice(array_reverse(xdebug_get_function_stack()), 1, -1);
  44. $trace = [];
  45. foreach ($xdebugTrace as $frame) {
  46. if (!isset($frame['function'])) {
  47. $frame['function'] = 'unknown';
  48. }
  49. // Xdebug < 2.1.1: https://bugs.xdebug.org/view.php?id=695
  50. if (!isset($frame['type']) || $frame['type'] === 'static') {
  51. $frame['type'] = '::';
  52. } elseif ($frame['type'] === 'dynamic') {
  53. $frame['type'] = '->';
  54. }
  55. // Xdebug has a different key name
  56. if (isset($frame['params']) && !isset($frame['args'])) {
  57. $frame['args'] = $frame['params'];
  58. }
  59. $trace[] = $frame;
  60. }
  61. $ref = new \ReflectionProperty('Exception', 'trace');
  62. $ref->setAccessible(true);
  63. $ref->setValue($this, $trace);
  64. }
  65. }
  66. /**
  67. * Ensures that Xdebug stack trace is available based on Xdebug version.
  68. * Idea taken from developer bishopb at https://github.com/rollbar/rollbar-php
  69. * @return bool
  70. */
  71. private function isXdebugStackAvailable()
  72. {
  73. if (!function_exists('xdebug_get_function_stack')) {
  74. return false;
  75. }
  76. // check for Xdebug being installed to ensure origin of xdebug_get_function_stack()
  77. $version = phpversion('xdebug');
  78. if ($version === false) {
  79. return false;
  80. }
  81. // Xdebug 2 and prior
  82. if (version_compare($version, '3.0.0', '<')) {
  83. return true;
  84. }
  85. // Xdebug 3 and later, proper mode is required
  86. return false !== strpos(ini_get('xdebug.mode'), 'develop');
  87. }
  88. /**
  89. * Returns if error is one of fatal type.
  90. *
  91. * @param array $error error got from error_get_last()
  92. * @return bool if error is one of fatal type
  93. */
  94. public static function isFatalError($error)
  95. {
  96. return isset($error['type']) && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, self::E_HHVM_FATAL_ERROR]);
  97. }
  98. /**
  99. * @return string the user-friendly name of this exception
  100. */
  101. public function getName()
  102. {
  103. static $names = [
  104. E_COMPILE_ERROR => 'PHP Compile Error',
  105. E_COMPILE_WARNING => 'PHP Compile Warning',
  106. E_CORE_ERROR => 'PHP Core Error',
  107. E_CORE_WARNING => 'PHP Core Warning',
  108. E_DEPRECATED => 'PHP Deprecated Warning',
  109. E_ERROR => 'PHP Fatal Error',
  110. E_NOTICE => 'PHP Notice',
  111. E_PARSE => 'PHP Parse Error',
  112. E_RECOVERABLE_ERROR => 'PHP Recoverable Error',
  113. E_STRICT => 'PHP Strict Warning',
  114. E_USER_DEPRECATED => 'PHP User Deprecated Warning',
  115. E_USER_ERROR => 'PHP User Error',
  116. E_USER_NOTICE => 'PHP User Notice',
  117. E_USER_WARNING => 'PHP User Warning',
  118. E_WARNING => 'PHP Warning',
  119. self::E_HHVM_FATAL_ERROR => 'HHVM Fatal Error',
  120. ];
  121. return isset($names[$this->getCode()]) ? $names[$this->getCode()] : 'Error';
  122. }
  123. }