ErrorException.php 4.1 KB

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