1
|
|
|
<?php |
2
|
|
|
namespace GuzzleHttp\Command\Exception; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
6
|
|
|
use GuzzleHttp\Command\CommandInterface; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Exception encountered while executing a command. |
12
|
|
|
*/ |
13
|
|
|
class CommandException extends \RuntimeException implements GuzzleException |
14
|
|
|
{ |
15
|
|
|
/** @var CommandInterface */ |
16
|
|
|
private $command; |
17
|
|
|
|
18
|
|
|
/** @var RequestInterface */ |
19
|
|
|
private $request; |
20
|
|
|
|
21
|
|
|
/** @var ResponseInterface */ |
22
|
|
|
private $response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param CommandInterface $command |
26
|
|
|
* @param \Exception $prev |
27
|
|
|
* @return CommandException |
28
|
|
|
*/ |
29
|
|
|
public static function fromPrevious(CommandInterface $command, \Exception $prev) |
30
|
|
|
{ |
31
|
|
|
// If the exception is already a command exception, return it. |
32
|
|
|
if ($prev instanceof self && $command === $prev->getCommand()) { |
33
|
|
|
return $prev; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// If the exception is a RequestException, get the Request and Response. |
37
|
|
|
$request = $response = null; |
38
|
|
|
if ($prev instanceof RequestException) { |
|
|
|
|
39
|
|
|
$request = $prev->getRequest(); |
40
|
|
|
$response = $prev->getResponse(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Throw a more specific exception for 4XX or 5XX responses. |
44
|
|
|
$class = self::class; |
45
|
|
|
$statusCode = $response ? $response->getStatusCode() : 0; |
46
|
|
|
if ($statusCode >= 400 && $statusCode < 500) { |
47
|
|
|
$class = CommandClientException::class; |
48
|
|
|
} elseif ($statusCode >= 500 && $statusCode < 600) { |
49
|
|
|
$class = CommandServerException::class; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Prepare the message. |
53
|
|
|
$message = 'There was an error executing the ' . $command->getName() |
54
|
|
|
. ' command: ' . $prev->getMessage(); |
55
|
|
|
|
56
|
|
|
// Create the exception. |
57
|
|
|
return new $class($message, $command, $prev, $request, $response); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $message Exception message |
62
|
|
|
* @param CommandInterface $command |
63
|
|
|
* @param \Exception $previous Previous exception (if any) |
64
|
|
|
* @param RequestInterface $request |
65
|
|
|
* @param ResponseInterface $response |
66
|
|
|
*/ |
67
|
|
|
public function __construct( |
68
|
|
|
$message, |
69
|
|
|
CommandInterface $command, |
70
|
|
|
\Exception $previous = null, |
71
|
|
|
RequestInterface $request = null, |
72
|
|
|
ResponseInterface $response = null |
73
|
|
|
) { |
74
|
|
|
$this->command = $command; |
75
|
|
|
$this->request = $request; |
76
|
|
|
$this->response = $response; |
77
|
|
|
parent::__construct($message, 0, $previous); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Gets the command that failed. |
82
|
|
|
* |
83
|
|
|
* @return CommandInterface |
84
|
|
|
*/ |
85
|
|
|
public function getCommand() |
86
|
|
|
{ |
87
|
|
|
return $this->command; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Gets the request that caused the exception |
92
|
|
|
* |
93
|
|
|
* @return RequestInterface|null |
94
|
|
|
*/ |
95
|
|
|
public function getRequest() |
96
|
|
|
{ |
97
|
|
|
return $this->request; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Gets the associated response |
102
|
|
|
* |
103
|
|
|
* @return ResponseInterface|null |
104
|
|
|
*/ |
105
|
|
|
public function getResponse() |
106
|
|
|
{ |
107
|
|
|
return $this->response; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.