ErrorException.php 3.2 KB
Newer Older
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5 6 7 8 9
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

10 11
use Yii;

12 13 14 15 16 17
/**
 * ErrorException represents a PHP error.
 *
 * @author Alexander Makarov <sam@rmcreative.ru>
 * @since 2.0
 */
18
class ErrorException extends \ErrorException
19
{
20 21 22 23 24 25 26 27 28 29 30 31
    /**
     * Constructs the exception.
     * @link http://php.net/manual/en/errorexception.construct.php
     * @param $message [optional]
     * @param $code [optional]
     * @param $severity [optional]
     * @param $filename [optional]
     * @param $lineno [optional]
     * @param $previous [optional]
     */
    public function __construct($message = '', $code = 0, $severity = 1, $filename = __FILE__, $lineno = __LINE__, \Exception $previous = null)
    {
32
        parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
33

34 35 36 37 38 39
        if (function_exists('xdebug_get_function_stack')) {
            $trace = array_slice(array_reverse(xdebug_get_function_stack()), 3, -1);
            foreach ($trace as &$frame) {
                if (!isset($frame['function'])) {
                    $frame['function'] = 'unknown';
                }
40

41 42 43 44 45 46
                // XDebug < 2.1.1: http://bugs.xdebug.org/view.php?id=695
                if (!isset($frame['type']) || $frame['type'] === 'static') {
                    $frame['type'] = '::';
                } elseif ($frame['type'] === 'dynamic') {
                    $frame['type'] = '->';
                }
47

48 49 50 51 52
                // XDebug has a different key name
                if (isset($frame['params']) && !isset($frame['args'])) {
                    $frame['args'] = $frame['params'];
                }
            }
53

54 55 56 57 58
            $ref = new \ReflectionProperty('Exception', 'trace');
            $ref->setAccessible(true);
            $ref->setValue($this, $trace);
        }
    }
59

60 61 62
    /**
     * Returns if error is one of fatal type.
     *
63
     * @param array $error error got from error_get_last()
64 65 66 67 68 69
     * @return boolean if error is one of fatal type
     */
    public static function isFatalError($error)
    {
        return isset($error['type']) && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING]);
    }
70

71 72 73 74 75
    /**
     * @return string the user-friendly name of this exception
     */
    public function getName()
    {
76
        static $names = [
77
            E_COMPILE_ERROR => 'PHP Compile Error',
78
            E_COMPILE_WARNING => 'PHP Compile Warning',
79 80
            E_CORE_ERROR => 'PHP Core Error',
            E_CORE_WARNING => 'PHP Core Warning',
81
            E_DEPRECATED => 'PHP Deprecated Warning',
82 83 84 85 86
            E_ERROR => 'PHP Fatal Error',
            E_NOTICE => 'PHP Notice',
            E_PARSE => 'PHP Parse Error',
            E_RECOVERABLE_ERROR => 'PHP Recoverable Error',
            E_STRICT => 'PHP Strict Warning',
87
            E_USER_DEPRECATED => 'PHP User Deprecated Warning',
88 89 90 91
            E_USER_ERROR => 'PHP User Error',
            E_USER_NOTICE => 'PHP User Notice',
            E_USER_WARNING => 'PHP User Warning',
            E_WARNING => 'PHP Warning',
92 93 94 95
        ];

        return isset($names[$this->getCode()]) ? $names[$this->getCode()] : 'Error';
    }
96
}