GuzzleHttpExceptionRequestException

HTTP Request exception.

Defined (1)

The class is defined in the following location(s).

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