Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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