1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Exception; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\Support\Stringy; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use AlibabaCloud\Client\SDK; |
8
|
|
|
use AlibabaCloud\Client\Result\Result; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ServerException |
12
|
|
|
* |
13
|
|
|
* @package AlibabaCloud\Client\Exception |
14
|
|
|
*/ |
15
|
|
|
class ServerException extends AlibabaCloudException |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $requestId; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Result |
25
|
|
|
*/ |
26
|
|
|
protected $result; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* ServerException constructor. |
30
|
|
|
* |
31
|
|
|
* @param Result|null $result |
32
|
|
|
* @param string $errorMessage |
33
|
|
|
* @param string $errorCode |
34
|
|
|
*/ |
35
|
28 |
|
public function __construct( |
36
|
|
|
Result $result, |
37
|
|
|
$errorMessage = SDK::RESPONSE_EMPTY, |
38
|
|
|
$errorCode = SDK::SERVICE_UNKNOWN_ERROR |
39
|
|
|
) { |
40
|
28 |
|
$this->result = $result; |
41
|
28 |
|
$this->errorMessage = $errorMessage; |
42
|
28 |
|
$this->errorCode = $errorCode; |
43
|
28 |
|
$this->resolvePropertiesByReturn(); |
44
|
28 |
|
$this->distinguishSignatureErrors(); |
45
|
28 |
|
$this->bodyAsErrorMessage(); |
46
|
|
|
|
47
|
28 |
|
parent::__construct( |
48
|
28 |
|
$this->getMessageString(), |
49
|
28 |
|
$this->result->getStatusCode() |
50
|
28 |
|
); |
51
|
28 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Resolve the error message based on the return of the server. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
28 |
|
private function resolvePropertiesByReturn() |
59
|
|
|
{ |
60
|
28 |
|
if (isset($this->result['message'])) { |
61
|
6 |
|
$this->errorMessage = $this->result['message']; |
62
|
6 |
|
$this->errorCode = $this->result['code']; |
63
|
6 |
|
} |
64
|
28 |
|
if (isset($this->result['Message'])) { |
65
|
18 |
|
$this->errorMessage = $this->result['Message']; |
66
|
18 |
|
$this->errorCode = $this->result['Code']; |
67
|
18 |
|
} |
68
|
28 |
|
if (isset($this->result['errorMsg'])) { |
69
|
1 |
|
$this->errorMessage = $this->result['errorMsg']; |
70
|
1 |
|
$this->errorCode = $this->result['errorCode']; |
71
|
1 |
|
} |
72
|
28 |
|
if (isset($this->result['requestId'])) { |
73
|
5 |
|
$this->requestId = $this->result['requestId']; |
74
|
5 |
|
} |
75
|
28 |
|
if (isset($this->result['RequestId'])) { |
76
|
16 |
|
$this->requestId = $this->result['RequestId']; |
77
|
16 |
|
} |
78
|
28 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* If the string to be signed are the same with server's, it is considered a credential error. |
82
|
|
|
*/ |
83
|
28 |
|
private function distinguishSignatureErrors() |
84
|
|
|
{ |
85
|
28 |
|
if ($this->result->getRequest() |
86
|
28 |
|
&& Stringy::contains($this->errorMessage, $this->result->getRequest()->stringToSign())) { |
87
|
1 |
|
$this->errorCode = 'InvalidAccessKeySecret'; |
88
|
1 |
|
$this->errorMessage = 'Specified Access Key Secret is not valid.'; |
89
|
1 |
|
} |
90
|
28 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* If the error message matches the default message and |
94
|
|
|
* the server has returned content, use the return content |
95
|
|
|
*/ |
96
|
28 |
|
private function bodyAsErrorMessage() |
97
|
|
|
{ |
98
|
28 |
|
$body = (string)$this->result->getBody(); |
99
|
28 |
|
if ($this->errorMessage === SDK::RESPONSE_EMPTY && $body) { |
100
|
1 |
|
$this->errorMessage = $body; |
101
|
1 |
|
} |
102
|
28 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get standard exception message. |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
28 |
|
private function getMessageString() |
110
|
|
|
{ |
111
|
28 |
|
$message = "$this->errorCode: $this->errorMessage RequestId: $this->requestId"; |
112
|
|
|
|
113
|
28 |
|
if ($this->getResult()->getRequest()) { |
114
|
24 |
|
$method = $this->getResult()->getRequest()->method; |
115
|
24 |
|
$uri = (string)$this->getResult()->getRequest()->uri; |
116
|
24 |
|
$message .= " $method \"$uri\""; |
117
|
24 |
|
if ($this->result) { |
118
|
24 |
|
$message .= ' ' . $this->result->getStatusCode(); |
119
|
24 |
|
} |
120
|
24 |
|
} |
121
|
|
|
|
122
|
28 |
|
return $message; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return Result |
127
|
|
|
*/ |
128
|
28 |
|
public function getResult() |
129
|
|
|
{ |
130
|
28 |
|
return $this->result; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
1 |
|
public function getRequestId() |
137
|
|
|
{ |
138
|
1 |
|
return $this->requestId; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @codeCoverageIgnore |
143
|
|
|
* @deprecated |
144
|
|
|
*/ |
145
|
|
|
public function getErrorType() |
146
|
|
|
{ |
147
|
|
|
return 'Server'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @codeCoverageIgnore |
152
|
|
|
* @deprecated |
153
|
|
|
*/ |
154
|
|
|
public function getHttpStatus() |
155
|
|
|
{ |
156
|
|
|
return $this->getResult()->getStatusCode(); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|