You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.9KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\base;
  8. use Yii;
  9. /**
  10. * ErrorException represents a PHP error.
  11. *
  12. * @author Alexander Makarov <sam@rmcreative.ru>
  13. * @since 2.0
  14. */
  15. class ErrorException extends \ErrorException
  16. {
  17. /**
  18. * This constant represents a fatal error in the HHVM engine.
  19. *
  20. * PHP Zend runtime won't call the error handler on fatals, HHVM will, with an error code of 16777217
  21. * We will handle fatal error a bit different on HHVM.
  22. * @see https://github.com/facebook/hhvm/blob/master/hphp/runtime/base/runtime-error.h#L62
  23. * @since 2.0.6
  24. */
  25. const E_HHVM_FATAL_ERROR = 16777217; // E_ERROR | (1 << 24)
  26. /**
  27. * Constructs the exception.
  28. * @link http://php.net/manual/en/errorexception.construct.php
  29. * @param $message [optional]
  30. * @param $code [optional]
  31. * @param $severity [optional]
  32. * @param $filename [optional]
  33. * @param $lineno [optional]
  34. * @param $previous [optional]
  35. */
  36. public function __construct($message = '', $code = 0, $severity = 1, $filename = __FILE__, $lineno = __LINE__, \Exception $previous = null)
  37. {
  38. parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
  39. if (function_exists('xdebug_get_function_stack')) {
  40. // XDebug trace can't be modified and used directly with PHP 7
  41. // @see https://github.com/yiisoft/yii2/pull/11723
  42. $xDebugTrace = array_slice(array_reverse(xdebug_get_function_stack()), 3, -1);
  43. $trace = [];
  44. foreach ($xDebugTrace as $frame) {
  45. if (!isset($frame['function'])) {
  46. $frame['function'] = 'unknown';
  47. }
  48. // XDebug < 2.1.1: http://bugs.xdebug.org/view.php?id=695
  49. if (!isset($frame['type']) || $frame['type'] === 'static') {
  50. $frame['type'] = '::';
  51. } elseif ($frame['type'] === 'dynamic') {
  52. $frame['type'] = '->';
  53. }
  54. // XDebug has a different key name
  55. if (isset($frame['params']) && !isset($frame['args'])) {
  56. $frame['args'] = $frame['params'];
  57. }
  58. $trace[] = $frame;
  59. }
  60. $ref = new \ReflectionProperty('Exception', 'trace');
  61. $ref->setAccessible(true);
  62. $ref->setValue($this, $trace);
  63. }
  64. }
  65. /**
  66. * Returns if error is one of fatal type.
  67. *
  68. * @param array $error error got from error_get_last()
  69. * @return boolean if error is one of fatal type
  70. */
  71. public static function isFatalError($error)
  72. {
  73. 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]);
  74. }
  75. /**
  76. * @return string the user-friendly name of this exception
  77. */
  78. public function getName()
  79. {
  80. static $names = [
  81. E_COMPILE_ERROR => 'PHP Compile Error',
  82. E_COMPILE_WARNING => 'PHP Compile Warning',
  83. E_CORE_ERROR => 'PHP Core Error',
  84. E_CORE_WARNING => 'PHP Core Warning',
  85. E_DEPRECATED => 'PHP Deprecated Warning',
  86. E_ERROR => 'PHP Fatal Error',
  87. E_NOTICE => 'PHP Notice',
  88. E_PARSE => 'PHP Parse Error',
  89. E_RECOVERABLE_ERROR => 'PHP Recoverable Error',
  90. E_STRICT => 'PHP Strict Warning',
  91. E_USER_DEPRECATED => 'PHP User Deprecated Warning',
  92. E_USER_ERROR => 'PHP User Error',
  93. E_USER_NOTICE => 'PHP User Notice',
  94. E_USER_WARNING => 'PHP User Warning',
  95. E_WARNING => 'PHP Warning',
  96. self::E_HHVM_FATAL_ERROR => 'HHVM Fatal Error',
  97. ];
  98. return isset($names[$this->getCode()]) ? $names[$this->getCode()] : 'Error';
  99. }
  100. }