PluginProbe
Gmail SMTP / trunk
Gmail SMTP vtrunk
1.2.3.21 1.2.3.20 trunk 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.9 1.2.0 1.2.3.14 1.2.3.15 1.2.3.16 1.2.3.18 1.2.3.5
gmail-smtp / google-api-php-client / vendor / guzzlehttp / guzzle / src / Exception / RequestException.php

RequestException.php in Gmail SMTP trunk, at google-api-php-client/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php

155 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @deprecated since 7.11. Create a RequestException directly instead.
50 */
51 public static function wrapException(RequestInterface $request, \Throwable $e): RequestException
52 {
53 \trigger_deprecation('guzzlehttp/guzzle', '7.11', '%s::wrapException() is deprecated and will be removed in 8.0. Create a %s directly instead.', self::class, self::class);
54
55 return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e);
56 }
57
58 /**
59 * Factory method to create a new exception with a normalized error message
60 *
61 * @param RequestInterface $request Request sent
62 * @param ResponseInterface $response Response received
63 * @param \Throwable|null $previous Previous exception
64 * @param array $handlerContext Optional handler context
65 * @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer
66 */
67 public static function create(
68 RequestInterface $request,
69 ?ResponseInterface $response = null,
70 ?\Throwable $previous = null,
71 array $handlerContext = [],
72 ?BodySummarizerInterface $bodySummarizer = null
73 ): self {
74 if (!$response) {
75 return new self(
76 'Error completing request',
77 $request,
78 null,
79 $previous,
80 $handlerContext
81 );
82 }
83
84 $level = (int) \floor($response->getStatusCode() / 100);
85 if ($level === 4) {
86 $label = 'Client error';
87 $className = ClientException::class;
88 } elseif ($level === 5) {
89 $label = 'Server error';
90 $className = ServerException::class;
91 } else {
92 $label = 'Unsuccessful request';
93 $className = __CLASS__;
94 }
95
96 $uri = \GuzzleHttp\Psr7\Utils::redactUserInfo($request->getUri());
97
98 // Client Error: `GET /` resulted in a `404 Not Found` response:
99 // <html> ... (truncated)
100 $message = \sprintf(
101 '%s: `%s %s` resulted in a `%s %s` response',
102 $label,
103 $request->getMethod(),
104 $uri->__toString(),
105 $response->getStatusCode(),
106 $response->getReasonPhrase()
107 );
108
109 $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response);
110
111 if ($summary !== null) {
112 $message .= ":\n{$summary}\n";
113 }
114
115 return new $className($message, $request, $response, $previous, $handlerContext);
116 }
117
118 /**
119 * Get the request that caused the exception
120 */
121 public function getRequest(): RequestInterface
122 {
123 return $this->request;
124 }
125
126 /**
127 * Get the associated response
128 */
129 public function getResponse(): ?ResponseInterface
130 {
131 return $this->response;
132 }
133
134 /**
135 * Check if a response was received
136 */
137 public function hasResponse(): bool
138 {
139 return $this->response !== null;
140 }
141
142 /**
143 * Get contextual information about the error from the underlying handler.
144 *
145 * The contents of this array will vary depending on which handler you are
146 * using. It may also be just an empty array. Relying on this data will
147 * couple you to a specific handler, but can give more debug information
148 * when needed.
149 */
150 public function getHandlerContext(): array
151 {
152 return $this->handlerContext;
153 }
154 }
155