| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Exception; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Api\Shape; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\CommandInterface; |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\HasDataTrait; |
| 8 |
use Dudlewebs\WPMCS\s3\Aws\HasMonitoringEventsTrait; |
| 9 |
use Dudlewebs\WPMCS\s3\Aws\MonitoringEventsInterface; |
| 10 |
use Dudlewebs\WPMCS\s3\Aws\ResponseContainerInterface; |
| 11 |
use Dudlewebs\WPMCS\s3\Aws\ResultInterface; |
| 12 |
use Dudlewebs\WPMCS\s3\JmesPath\Env as JmesPath; |
| 13 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface; |
| 14 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 15 |
use Throwable; |
| 16 |
/** |
| 17 |
* Represents an AWS exception that is thrown when a command fails. |
| 18 |
*/ |
| 19 |
class AwsException extends \RuntimeException implements MonitoringEventsInterface, ResponseContainerInterface, \ArrayAccess |
| 20 |
{ |
| 21 |
use HasDataTrait; |
| 22 |
use HasMonitoringEventsTrait; |
| 23 |
/** @var ResponseInterface */ |
| 24 |
private $response; |
| 25 |
private $request; |
| 26 |
private $result; |
| 27 |
private $command; |
| 28 |
private $requestId; |
| 29 |
private $errorType; |
| 30 |
private $errorCode; |
| 31 |
private $errorShape; |
| 32 |
private $connectionError; |
| 33 |
private $transferInfo; |
| 34 |
private $errorMessage; |
| 35 |
private $maxRetriesExceeded; |
| 36 |
/** |
| 37 |
* @param string $message Exception message |
| 38 |
* @param CommandInterface $command |
| 39 |
* @param array $context Exception context |
| 40 |
* @param Throwable|null $previous Previous exception (if any) |
| 41 |
*/ |
| 42 |
public function __construct($message, CommandInterface $command, array $context = [], ?Throwable $previous = null) |
| 43 |
{ |
| 44 |
$this->data = $context['body'] ?? []; |
| 45 |
$this->command = $command; |
| 46 |
$this->response = $context['response'] ?? null; |
| 47 |
$this->request = $context['request'] ?? null; |
| 48 |
$this->requestId = $context['request_id'] ?? null; |
| 49 |
$this->errorType = $context['type'] ?? null; |
| 50 |
$this->errorCode = $context['code'] ?? null; |
| 51 |
$this->errorShape = $context['error_shape'] ?? null; |
| 52 |
$this->connectionError = !empty($context['connection_error']); |
| 53 |
$this->result = $context['result'] ?? null; |
| 54 |
$this->transferInfo = $context['transfer_stats'] ?? []; |
| 55 |
$this->errorMessage = $context['message'] ?? null; |
| 56 |
$this->monitoringEvents = []; |
| 57 |
$this->maxRetriesExceeded = \false; |
| 58 |
parent::__construct($message, 0, $previous); |
| 59 |
} |
| 60 |
public function __toString() |
| 61 |
{ |
| 62 |
if (!$this->getPrevious()) { |
| 63 |
return parent::__toString(); |
| 64 |
} |
| 65 |
// PHP strangely shows the innermost exception first before the outer |
| 66 |
// exception message. It also has a default character limit for |
| 67 |
// exception message strings such that the "next" exception (this one) |
| 68 |
// might not even get shown, causing developers to attempt to catch |
| 69 |
// the inner exception instead of the actual exception because they |
| 70 |
// can't see the outer exception's __toString output. |
| 71 |
return \sprintf("exception '%s' with message '%s'\n\n%s", \get_class($this), $this->getMessage(), parent::__toString()); |
| 72 |
} |
| 73 |
/** |
| 74 |
* Get the command that was executed. |
| 75 |
* |
| 76 |
* @return CommandInterface |
| 77 |
*/ |
| 78 |
public function getCommand() |
| 79 |
{ |
| 80 |
return $this->command; |
| 81 |
} |
| 82 |
/** |
| 83 |
* Get the concise error message if any. |
| 84 |
* |
| 85 |
* @return string|null |
| 86 |
*/ |
| 87 |
public function getAwsErrorMessage() |
| 88 |
{ |
| 89 |
return $this->errorMessage; |
| 90 |
} |
| 91 |
/** |
| 92 |
* Get the sent HTTP request if any. |
| 93 |
* |
| 94 |
* @return RequestInterface|null |
| 95 |
*/ |
| 96 |
public function getRequest() |
| 97 |
{ |
| 98 |
return $this->request; |
| 99 |
} |
| 100 |
/** |
| 101 |
* Get the received HTTP response if any. |
| 102 |
* |
| 103 |
* @return ResponseInterface|null |
| 104 |
*/ |
| 105 |
public function getResponse() |
| 106 |
{ |
| 107 |
return $this->response; |
| 108 |
} |
| 109 |
/** |
| 110 |
* Get the result of the exception if available |
| 111 |
* |
| 112 |
* @return ResultInterface|null |
| 113 |
*/ |
| 114 |
public function getResult() |
| 115 |
{ |
| 116 |
return $this->result; |
| 117 |
} |
| 118 |
/** |
| 119 |
* Returns true if this is a connection error. |
| 120 |
* |
| 121 |
* @return bool |
| 122 |
*/ |
| 123 |
public function isConnectionError() |
| 124 |
{ |
| 125 |
return $this->connectionError; |
| 126 |
} |
| 127 |
/** |
| 128 |
* If available, gets the HTTP status code of the corresponding response |
| 129 |
* |
| 130 |
* @return int|null |
| 131 |
*/ |
| 132 |
public function getStatusCode() |
| 133 |
{ |
| 134 |
return $this->response ? $this->response->getStatusCode() : null; |
| 135 |
} |
| 136 |
/** |
| 137 |
* Get the request ID of the error. This value is only present if a |
| 138 |
* response was received and is not present in the event of a networking |
| 139 |
* error. |
| 140 |
* |
| 141 |
* @return string|null Returns null if no response was received |
| 142 |
*/ |
| 143 |
public function getAwsRequestId() |
| 144 |
{ |
| 145 |
return $this->requestId; |
| 146 |
} |
| 147 |
/** |
| 148 |
* Get the AWS error type. |
| 149 |
* |
| 150 |
* @return string|null Returns null if no response was received |
| 151 |
*/ |
| 152 |
public function getAwsErrorType() |
| 153 |
{ |
| 154 |
return $this->errorType; |
| 155 |
} |
| 156 |
/** |
| 157 |
* Get the AWS error code. |
| 158 |
* |
| 159 |
* @return string|null Returns null if no response was received |
| 160 |
*/ |
| 161 |
public function getAwsErrorCode() |
| 162 |
{ |
| 163 |
return $this->errorCode; |
| 164 |
} |
| 165 |
/** |
| 166 |
* Get the AWS error shape. |
| 167 |
* |
| 168 |
* @return Shape|null Returns null if no response was received |
| 169 |
*/ |
| 170 |
public function getAwsErrorShape() |
| 171 |
{ |
| 172 |
return $this->errorShape; |
| 173 |
} |
| 174 |
/** |
| 175 |
* Get all transfer information as an associative array if no $name |
| 176 |
* argument is supplied, or gets a specific transfer statistic if |
| 177 |
* a $name attribute is supplied (e.g., 'retries_attempted'). |
| 178 |
* |
| 179 |
* @param string $name Name of the transfer stat to retrieve |
| 180 |
* |
| 181 |
* @return mixed|null|array |
| 182 |
*/ |
| 183 |
public function getTransferInfo($name = null) |
| 184 |
{ |
| 185 |
if (!$name) { |
| 186 |
return $this->transferInfo; |
| 187 |
} |
| 188 |
return $this->transferInfo[$name] ?? null; |
| 189 |
} |
| 190 |
/** |
| 191 |
* Replace the transfer information associated with an exception. |
| 192 |
* |
| 193 |
* @param array $info |
| 194 |
*/ |
| 195 |
public function setTransferInfo(array $info) |
| 196 |
{ |
| 197 |
$this->transferInfo = $info; |
| 198 |
} |
| 199 |
/** |
| 200 |
* Returns whether the max number of retries is exceeded. |
| 201 |
* |
| 202 |
* @return bool |
| 203 |
*/ |
| 204 |
public function isMaxRetriesExceeded() |
| 205 |
{ |
| 206 |
return $this->maxRetriesExceeded; |
| 207 |
} |
| 208 |
/** |
| 209 |
* Sets the flag for max number of retries exceeded. |
| 210 |
*/ |
| 211 |
public function setMaxRetriesExceeded() |
| 212 |
{ |
| 213 |
$this->maxRetriesExceeded = \true; |
| 214 |
} |
| 215 |
public function hasKey($name) |
| 216 |
{ |
| 217 |
return isset($this->data[$name]); |
| 218 |
} |
| 219 |
public function get($key) |
| 220 |
{ |
| 221 |
return $this[$key]; |
| 222 |
} |
| 223 |
public function search($expression) |
| 224 |
{ |
| 225 |
return JmesPath::search($expression, $this->toArray()); |
| 226 |
} |
| 227 |
} |
| 228 |
|