| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Exception; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface; |
| 6 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 7 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface; |
| 8 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\UriInterface; |
| 9 |
/** |
| 10 |
* HTTP Request exception |
| 11 |
*/ |
| 12 |
class RequestException extends TransferException |
| 13 |
{ |
| 14 |
/** @var RequestInterface */ |
| 15 |
private $request; |
| 16 |
/** @var ResponseInterface|null */ |
| 17 |
private $response; |
| 18 |
/** @var array */ |
| 19 |
private $handlerContext; |
| 20 |
public function __construct($message, RequestInterface $request, ResponseInterface $response = null, \Exception $previous = null, array $handlerContext = []) |
| 21 |
{ |
| 22 |
// Set the code of the exception if the response is set and not future. |
| 23 |
$code = $response && !$response instanceof PromiseInterface ? $response->getStatusCode() : 0; |
| 24 |
parent::__construct($message, $code, $previous); |
| 25 |
$this->request = $request; |
| 26 |
$this->response = $response; |
| 27 |
$this->handlerContext = $handlerContext; |
| 28 |
} |
| 29 |
/** |
| 30 |
* Wrap non-RequestExceptions with a RequestException |
| 31 |
* |
| 32 |
* @param RequestInterface $request |
| 33 |
* @param \Exception $e |
| 34 |
* |
| 35 |
* @return RequestException |
| 36 |
*/ |
| 37 |
public static function wrapException(RequestInterface $request, \Exception $e) |
| 38 |
{ |
| 39 |
return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); |
| 40 |
} |
| 41 |
/** |
| 42 |
* Factory method to create a new exception with a normalized error message |
| 43 |
* |
| 44 |
* @param RequestInterface $request Request |
| 45 |
* @param ResponseInterface $response Response received |
| 46 |
* @param \Exception $previous Previous exception |
| 47 |
* @param array $ctx Optional handler context. |
| 48 |
* |
| 49 |
* @return self |
| 50 |
*/ |
| 51 |
public static function create(RequestInterface $request, ResponseInterface $response = null, \Exception $previous = null, array $ctx = []) |
| 52 |
{ |
| 53 |
if (!$response) { |
| 54 |
return new self('Error completing request', $request, null, $previous, $ctx); |
| 55 |
} |
| 56 |
$level = (int) \floor($response->getStatusCode() / 100); |
| 57 |
if ($level === 4) { |
| 58 |
$label = 'Client error'; |
| 59 |
$className = ClientException::class; |
| 60 |
} elseif ($level === 5) { |
| 61 |
$label = 'Server error'; |
| 62 |
$className = ServerException::class; |
| 63 |
} else { |
| 64 |
$label = 'Unsuccessful request'; |
| 65 |
$className = __CLASS__; |
| 66 |
} |
| 67 |
$uri = $request->getUri(); |
| 68 |
$uri = static::obfuscateUri($uri); |
| 69 |
// Client Error: `GET /` resulted in a `404 Not Found` response: |
| 70 |
// <html> ... (truncated) |
| 71 |
$message = \sprintf('%s: `%s %s` resulted in a `%s %s` response', $label, $request->getMethod(), $uri, $response->getStatusCode(), $response->getReasonPhrase()); |
| 72 |
$summary = static::getResponseBodySummary($response); |
| 73 |
if ($summary !== null) { |
| 74 |
$message .= ":\n{$summary}\n"; |
| 75 |
} |
| 76 |
return new $className($message, $request, $response, $previous, $ctx); |
| 77 |
} |
| 78 |
/** |
| 79 |
* Get a short summary of the response |
| 80 |
* |
| 81 |
* Will return `null` if the response is not printable. |
| 82 |
* |
| 83 |
* @param ResponseInterface $response |
| 84 |
* |
| 85 |
* @return string|null |
| 86 |
*/ |
| 87 |
public static function getResponseBodySummary(ResponseInterface $response) |
| 88 |
{ |
| 89 |
return \Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\get_message_body_summary($response); |
| 90 |
} |
| 91 |
/** |
| 92 |
* Obfuscates URI if there is a username and a password present |
| 93 |
* |
| 94 |
* @param UriInterface $uri |
| 95 |
* |
| 96 |
* @return UriInterface |
| 97 |
*/ |
| 98 |
private static function obfuscateUri(UriInterface $uri) |
| 99 |
{ |
| 100 |
$userInfo = $uri->getUserInfo(); |
| 101 |
if (\false !== ($pos = \strpos($userInfo, ':'))) { |
| 102 |
return $uri->withUserInfo(\substr($userInfo, 0, $pos), '***'); |
| 103 |
} |
| 104 |
return $uri; |
| 105 |
} |
| 106 |
/** |
| 107 |
* Get the request that caused the exception |
| 108 |
* |
| 109 |
* @return RequestInterface |
| 110 |
*/ |
| 111 |
public function getRequest() |
| 112 |
{ |
| 113 |
return $this->request; |
| 114 |
} |
| 115 |
/** |
| 116 |
* Get the associated response |
| 117 |
* |
| 118 |
* @return ResponseInterface|null |
| 119 |
*/ |
| 120 |
public function getResponse() |
| 121 |
{ |
| 122 |
return $this->response; |
| 123 |
} |
| 124 |
/** |
| 125 |
* Check if a response was received |
| 126 |
* |
| 127 |
* @return bool |
| 128 |
*/ |
| 129 |
public function hasResponse() |
| 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 |
* @return array |
| 142 |
*/ |
| 143 |
public function getHandlerContext() |
| 144 |
{ |
| 145 |
return $this->handlerContext; |
| 146 |
} |
| 147 |
} |
| 148 |
|