Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Exception/AwsException.php

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