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