| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace GuzzleHttp\Exception;
|
|
|
4 |
|
|
|
5 |
use GuzzleHttp\BodySummarizer;
|
|
|
6 |
use GuzzleHttp\BodySummarizerInterface;
|
|
|
7 |
use Psr\Http\Client\RequestExceptionInterface;
|
|
|
8 |
use Psr\Http\Message\RequestInterface;
|
|
|
9 |
use Psr\Http\Message\ResponseInterface;
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* HTTP Request exception
|
|
|
13 |
*/
|
|
|
14 |
class RequestException extends TransferException implements RequestExceptionInterface
|
|
|
15 |
{
|
|
|
16 |
/**
|
|
|
17 |
* @var RequestInterface
|
|
|
18 |
*/
|
|
|
19 |
private $request;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* @var ResponseInterface|null
|
|
|
23 |
*/
|
|
|
24 |
private $response;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* @var array
|
|
|
28 |
*/
|
|
|
29 |
private $handlerContext;
|
|
|
30 |
|
|
|
31 |
public function __construct(
|
|
|
32 |
string $message,
|
|
|
33 |
RequestInterface $request,
|
| 1441 |
ariadna |
34 |
?ResponseInterface $response = null,
|
|
|
35 |
?\Throwable $previous = null,
|
| 1 |
efrain |
36 |
array $handlerContext = []
|
|
|
37 |
) {
|
|
|
38 |
// Set the code of the exception if the response is set and not future.
|
|
|
39 |
$code = $response ? $response->getStatusCode() : 0;
|
|
|
40 |
parent::__construct($message, $code, $previous);
|
|
|
41 |
$this->request = $request;
|
|
|
42 |
$this->response = $response;
|
|
|
43 |
$this->handlerContext = $handlerContext;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Wrap non-RequestExceptions with a RequestException
|
|
|
48 |
*/
|
|
|
49 |
public static function wrapException(RequestInterface $request, \Throwable $e): RequestException
|
|
|
50 |
{
|
|
|
51 |
return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Factory method to create a new exception with a normalized error message
|
|
|
56 |
*
|
|
|
57 |
* @param RequestInterface $request Request sent
|
|
|
58 |
* @param ResponseInterface $response Response received
|
|
|
59 |
* @param \Throwable|null $previous Previous exception
|
|
|
60 |
* @param array $handlerContext Optional handler context
|
|
|
61 |
* @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer
|
|
|
62 |
*/
|
|
|
63 |
public static function create(
|
|
|
64 |
RequestInterface $request,
|
| 1441 |
ariadna |
65 |
?ResponseInterface $response = null,
|
|
|
66 |
?\Throwable $previous = null,
|
| 1 |
efrain |
67 |
array $handlerContext = [],
|
| 1441 |
ariadna |
68 |
?BodySummarizerInterface $bodySummarizer = null
|
| 1 |
efrain |
69 |
): self {
|
|
|
70 |
if (!$response) {
|
|
|
71 |
return new self(
|
|
|
72 |
'Error completing request',
|
|
|
73 |
$request,
|
|
|
74 |
null,
|
|
|
75 |
$previous,
|
|
|
76 |
$handlerContext
|
|
|
77 |
);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$level = (int) \floor($response->getStatusCode() / 100);
|
|
|
81 |
if ($level === 4) {
|
|
|
82 |
$label = 'Client error';
|
|
|
83 |
$className = ClientException::class;
|
|
|
84 |
} elseif ($level === 5) {
|
|
|
85 |
$label = 'Server error';
|
|
|
86 |
$className = ServerException::class;
|
|
|
87 |
} else {
|
|
|
88 |
$label = 'Unsuccessful request';
|
|
|
89 |
$className = __CLASS__;
|
|
|
90 |
}
|
|
|
91 |
|
| 1441 |
ariadna |
92 |
$uri = \GuzzleHttp\Psr7\Utils::redactUserInfo($request->getUri());
|
| 1 |
efrain |
93 |
|
|
|
94 |
// Client Error: `GET /` resulted in a `404 Not Found` response:
|
|
|
95 |
// <html> ... (truncated)
|
|
|
96 |
$message = \sprintf(
|
|
|
97 |
'%s: `%s %s` resulted in a `%s %s` response',
|
|
|
98 |
$label,
|
|
|
99 |
$request->getMethod(),
|
|
|
100 |
$uri->__toString(),
|
|
|
101 |
$response->getStatusCode(),
|
|
|
102 |
$response->getReasonPhrase()
|
|
|
103 |
);
|
|
|
104 |
|
|
|
105 |
$summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response);
|
|
|
106 |
|
|
|
107 |
if ($summary !== null) {
|
|
|
108 |
$message .= ":\n{$summary}\n";
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
return new $className($message, $request, $response, $previous, $handlerContext);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Get the request that caused the exception
|
|
|
116 |
*/
|
|
|
117 |
public function getRequest(): RequestInterface
|
|
|
118 |
{
|
|
|
119 |
return $this->request;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Get the associated response
|
|
|
124 |
*/
|
|
|
125 |
public function getResponse(): ?ResponseInterface
|
|
|
126 |
{
|
|
|
127 |
return $this->response;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Check if a response was received
|
|
|
132 |
*/
|
|
|
133 |
public function hasResponse(): bool
|
|
|
134 |
{
|
|
|
135 |
return $this->response !== null;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Get contextual information about the error from the underlying handler.
|
|
|
140 |
*
|
|
|
141 |
* The contents of this array will vary depending on which handler you are
|
|
|
142 |
* using. It may also be just an empty array. Relying on this data will
|
|
|
143 |
* couple you to a specific handler, but can give more debug information
|
|
|
144 |
* when needed.
|
|
|
145 |
*/
|
|
|
146 |
public function getHandlerContext(): array
|
|
|
147 |
{
|
|
|
148 |
return $this->handlerContext;
|
|
|
149 |
}
|
|
|
150 |
}
|