2 namespace GuzzleHttp\Exception;
4 use Psr\Http\Message\RequestInterface;
5 use Psr\Http\Message\ResponseInterface;
6 use GuzzleHttp\Promise\PromiseInterface;
7 use Psr\Http\Message\UriInterface;
10 * HTTP Request exception
12 class RequestException extends TransferException
14 /** @var RequestInterface */
17 /** @var ResponseInterface */
21 private $handlerContext;
23 public function __construct(
25 RequestInterface $request,
26 ResponseInterface $response = null,
27 \Exception $previous = null,
28 array $handlerContext = []
30 // Set the code of the exception if the response is set and not future.
31 $code = $response && !($response instanceof PromiseInterface)
32 ? $response->getStatusCode()
34 parent::__construct($message, $code, $previous);
35 $this->request = $request;
36 $this->response = $response;
37 $this->handlerContext = $handlerContext;
41 * Wrap non-RequestExceptions with a RequestException
43 * @param RequestInterface $request
44 * @param \Exception $e
46 * @return RequestException
48 public static function wrapException(RequestInterface $request, \Exception $e)
50 return $e instanceof RequestException
52 : new RequestException($e->getMessage(), $request, null, $e);
56 * Factory method to create a new exception with a normalized error message
58 * @param RequestInterface $request Request
59 * @param ResponseInterface $response Response received
60 * @param \Exception $previous Previous exception
61 * @param array $ctx Optional handler context.
65 public static function create(
66 RequestInterface $request,
67 ResponseInterface $response = null,
68 \Exception $previous = null,
73 'Error completing request',
81 $level = (int) floor($response->getStatusCode() / 100);
83 $label = 'Client error';
84 $className = ClientException::class;
85 } elseif ($level === 5) {
86 $label = 'Server error';
87 $className = ServerException::class;
89 $label = 'Unsuccessful request';
90 $className = __CLASS__;
93 $uri = $request->getUri();
94 $uri = static::obfuscateUri($uri);
96 // Client Error: `GET /` resulted in a `404 Not Found` response:
97 // <html> ... (truncated)
99 '%s: `%s %s` resulted in a `%s %s` response',
101 $request->getMethod(),
103 $response->getStatusCode(),
104 $response->getReasonPhrase()
107 $summary = static::getResponseBodySummary($response);
109 if ($summary !== null) {
110 $message .= ":\n{$summary}\n";
113 return new $className($message, $request, $response, $previous, $ctx);
117 * Get a short summary of the response
119 * Will return `null` if the response is not printable.
121 * @param ResponseInterface $response
123 * @return string|null
125 public static function getResponseBodySummary(ResponseInterface $response)
127 $body = $response->getBody();
129 if (!$body->isSeekable()) {
133 $size = $body->getSize();
139 $summary = $body->read(120);
143 $summary .= ' (truncated...)';
146 // Matches any printable character, including unicode characters:
147 // letters, marks, numbers, punctuation, spacing, and separators.
148 if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/', $summary)) {
156 * Obfuscates URI if there is an username and a password present
158 * @param UriInterface $uri
160 * @return UriInterface
162 private static function obfuscateUri($uri)
164 $userInfo = $uri->getUserInfo();
166 if (false !== ($pos = strpos($userInfo, ':'))) {
167 return $uri->withUserInfo(substr($userInfo, 0, $pos), '***');
174 * Get the request that caused the exception
176 * @return RequestInterface
178 public function getRequest()
180 return $this->request;
184 * Get the associated response
186 * @return ResponseInterface|null
188 public function getResponse()
190 return $this->response;
194 * Check if a response was received
198 public function hasResponse()
200 return $this->response !== null;
204 * Get contextual information about the error from the underlying handler.
206 * The contents of this array will vary depending on which handler you are
207 * using. It may also be just an empty array. Relying on this data will
208 * couple you to a specific handler, but can give more debug information
213 public function getHandlerContext()
215 return $this->handlerContext;