Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 200 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Psy\Exception;
13
 
14
/**
15
 * A custom error Exception for Psy with a formatted $message.
16
 */
17
class ErrorException extends \ErrorException implements Exception
18
{
19
    private $rawMessage;
20
 
21
    /**
22
     * Construct a Psy ErrorException.
23
     *
24
     * @param string          $message  (default: "")
25
     * @param int             $code     (default: 0)
26
     * @param int             $severity (default: 1)
27
     * @param string|null     $filename (default: null)
28
     * @param int|null        $lineno   (default: null)
29
     * @param \Throwable|null $previous (default: null)
30
     */
31
    public function __construct($message = '', $code = 0, $severity = 1, $filename = null, $lineno = null, \Throwable $previous = null)
32
    {
33
        $this->rawMessage = $message;
34
 
35
        if (!empty($filename) && \preg_match('{Psy[/\\\\]ExecutionLoop}', $filename)) {
36
            $filename = '';
37
        }
38
 
39
        switch ($severity) {
40
            case \E_STRICT:
41
                $type = 'Strict error';
42
                break;
43
 
44
            case \E_NOTICE:
45
            case \E_USER_NOTICE:
46
                $type = 'Notice';
47
                break;
48
 
49
            case \E_WARNING:
50
            case \E_CORE_WARNING:
51
            case \E_COMPILE_WARNING:
52
            case \E_USER_WARNING:
53
                $type = 'Warning';
54
                break;
55
 
56
            case \E_DEPRECATED:
57
            case \E_USER_DEPRECATED:
58
                $type = 'Deprecated';
59
                break;
60
 
61
            case \E_RECOVERABLE_ERROR:
62
                $type = 'Recoverable fatal error';
63
                break;
64
 
65
            default:
66
                $type = 'Error';
67
                break;
68
        }
69
 
70
        $message = \sprintf('PHP %s:  %s%s on line %d', $type, $message, $filename ? ' in '.$filename : '', $lineno);
71
        parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
72
    }
73
 
74
    /**
75
     * Get the raw (unformatted) message for this error.
76
     */
77
    public function getRawMessage(): string
78
    {
79
        return $this->rawMessage;
80
    }
81
 
82
    /**
83
     * Helper for throwing an ErrorException.
84
     *
85
     * This allows us to:
86
     *
87
     *     set_error_handler([ErrorException::class, 'throwException']);
88
     *
89
     * @throws self
90
     *
91
     * @param int    $errno   Error type
92
     * @param string $errstr  Message
93
     * @param string $errfile Filename
94
     * @param int    $errline Line number
95
     */
96
    public static function throwException($errno, $errstr, $errfile, $errline)
97
    {
98
        throw new self($errstr, 0, $errno, $errfile, $errline);
99
    }
100
 
101
    /**
102
     * Create an ErrorException from an Error.
103
     *
1294 lars 104
     * @deprecated PsySH no longer wraps Errors
148 lars 105
     *
106
     * @param \Error $e
107
     */
108
    public static function fromError(\Error $e): self
109
    {
110
        return new self($e->getMessage(), $e->getCode(), 1, $e->getFile(), $e->getLine(), $e);
111
    }
112
}