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.

97 lines
3.2KB

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