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