comparison vendor/psy/psysh/src/Psy/Exception/ErrorException.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of Psy Shell.
5 *
6 * (c) 2012-2017 Justin Hileman
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 $filename (default: null)
28 * @param int $lineno (default: null)
29 * @param Exception $previous (default: null)
30 */
31 public function __construct($message = '', $code = 0, $severity = 1, $filename = null, $lineno = null, $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 * @return string
78 */
79 public function getRawMessage()
80 {
81 return $this->rawMessage;
82 }
83
84 /**
85 * Helper for throwing an ErrorException.
86 *
87 * This allows us to:
88 *
89 * set_error_handler(array('Psy\Exception\ErrorException', 'throwException'));
90 *
91 * @throws ErrorException
92 *
93 * @param int $errno Error type
94 * @param string $errstr Message
95 * @param string $errfile Filename
96 * @param int $errline Line number
97 */
98 public static function throwException($errno, $errstr, $errfile, $errline)
99 {
100 throw new self($errstr, 0, $errno, $errfile, $errline);
101 }
102
103 /**
104 * Create an ErrorException from an Error.
105 *
106 * @param \Error $e
107 *
108 * @return ErrorException
109 */
110 public static function fromError(\Error $e)
111 {
112 return new self($e->getMessage(), $e->getCode(), 1, $e->getFile(), $e->getLine(), $e);
113 }
114 }