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 = __NAMESPACE__ . '\\ClientException';
85 } elseif ($level === 5) {
86 $label = 'Server error';
87 $className = __NAMESPACE__ . '\\ServerException';
89 $label = 'Unsuccessful request';
90 $className = __CLASS__;
93 $uri = $request->getUri();
94 $uri = static::obfuscateUri($uri);
96 // Server Error: `GET /` resulted in a `404 Not Found` response:
97 // <html> ... (truncated)
99 '%s: `%s` resulted in a `%s` response',
101 $request->getMethod() . ' ' . $uri,
102 $response->getStatusCode() . ' ' . $response->getReasonPhrase()
105 $summary = static::getResponseBodySummary($response);
107 if ($summary !== null) {
108 $message .= ":\n{$summary}\n";
111 return new $className($message, $request, $response, $previous, $ctx);
115 * Get a short summary of the response
117 * Will return `null` if the response is not printable.
119 * @param ResponseInterface $response
121 * @return string|null
123 public static function getResponseBodySummary(ResponseInterface $response)
125 $body = $response->getBody();
127 if (!$body->isSeekable()) {
131 $size = $body->getSize();
132 $summary = $body->read(120);
136 $summary .= ' (truncated...)';
139 // Matches any printable character, including unicode characters:
140 // letters, marks, numbers, punctuation, spacing, and separators.
141 if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/', $summary)) {
149 * Obfuscates URI if there is an username and a password present
151 * @param UriInterface $uri
153 * @return UriInterface
155 private static function obfuscateUri($uri)
157 $userInfo = $uri->getUserInfo();
159 if (false !== ($pos = strpos($userInfo, ':'))) {
160 return $uri->withUserInfo(substr($userInfo, 0, $pos), '***');
167 * Get the request that caused the exception
169 * @return RequestInterface
171 public function getRequest()
173 return $this->request;
177 * Get the associated response
179 * @return ResponseInterface|null
181 public function getResponse()
183 return $this->response;
187 * Check if a response was received
191 public function hasResponse()
193 return $this->response !== null;
197 * Get contextual information about the error from the underlying handler.
199 * The contents of this array will vary depending on which handler you are
200 * using. It may also be just an empty array. Relying on this data will
201 * couple you to a specific handler, but can give more debug information
206 public function getHandlerContext()
208 return $this->handlerContext;