vendor/guzzlehttp/guzzle/src/Exception/RequestException.php line 113

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