ErrorException.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <[email protected]>
  13. * @since 2.0
  14. */
  15. class ErrorException extends Exception
  16. {
  17. protected $severity;
  18. /**
  19. * Constructs the exception.
  20. * @link http://php.net/manual/en/errorexception.construct.php
  21. * @param $message [optional]
  22. * @param $code [optional]
  23. * @param $severity [optional]
  24. * @param $filename [optional]
  25. * @param $lineno [optional]
  26. * @param $previous [optional]
  27. */
  28. public function __construct($message = '', $code = 0, $severity = 1, $filename = __FILE__, $lineno = __LINE__, \Exception $previous = null)
  29. {
  30. parent::__construct($message, $code, $previous);
  31. $this->severity = $severity;
  32. $this->file = $filename;
  33. $this->line = $lineno;
  34. if (function_exists('xdebug_get_function_stack')) {
  35. $trace = array_slice(array_reverse(xdebug_get_function_stack()), 3, -1);
  36. foreach ($trace as &$frame) {
  37. if (!isset($frame['function'])) {
  38. $frame['function'] = 'unknown';
  39. }
  40. // XDebug < 2.1.1: http://bugs.xdebug.org/view.php?id=695
  41. if (!isset($frame['type']) || $frame['type'] === 'static') {
  42. $frame['type'] = '::';
  43. } elseif ($frame['type'] === 'dynamic') {
  44. $frame['type'] = '->';
  45. }
  46. // XDebug has a different key name
  47. if (isset($frame['params']) && !isset($frame['args'])) {
  48. $frame['args'] = $frame['params'];
  49. }
  50. }
  51. $ref = new \ReflectionProperty('Exception', 'trace');
  52. $ref->setAccessible(true);
  53. $ref->setValue($this, $trace);
  54. }
  55. }
  56. /**
  57. * Returns if error is one of fatal type.
  58. *
  59. * @param array $error error got from error_get_last()
  60. * @return bool if error is one of fatal type
  61. */
  62. public static function isFatalError($error)
  63. {
  64. return isset($error['type']) && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING]);
  65. }
  66. /**
  67. * Gets the exception severity.
  68. * @link http://php.net/manual/en/errorexception.getseverity.php
  69. * @return int the severity level of the exception.
  70. */
  71. final public function getSeverity()
  72. {
  73. return $this->severity;
  74. }
  75. /**
  76. * @return string the user-friendly name of this exception
  77. */
  78. public function getName()
  79. {
  80. $names = [
  81. E_ERROR => 'PHP Fatal Error',
  82. E_PARSE => 'PHP Parse Error',
  83. E_CORE_ERROR => 'PHP Core Error',
  84. E_COMPILE_ERROR => 'PHP Compile Error',
  85. E_USER_ERROR => 'PHP User Error',
  86. E_WARNING => 'PHP Warning',
  87. E_CORE_WARNING => 'PHP Core Warning',
  88. E_COMPILE_WARNING => 'PHP Compile Warning',
  89. E_USER_WARNING => 'PHP User Warning',
  90. E_STRICT => 'PHP Strict Warning',
  91. E_NOTICE => 'PHP Notice',
  92. E_RECOVERABLE_ERROR => 'PHP Recoverable Error',
  93. E_DEPRECATED => 'PHP Deprecated Warning',
  94. ];
  95. return isset($names[$this->getCode()]) ? $names[$this->getCode()] : 'Error';
  96. }
  97. }