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