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