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