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
|
46 |
|
public function __construct(Result $result, $errorMessage = '', $errorCode = '') |
34
|
|
|
{ |
35
|
46 |
|
$this->result = $result; |
36
|
46 |
|
$this->settingProperties(); |
37
|
|
|
|
38
|
46 |
|
if ($errorCode !== '') { |
39
|
1 |
|
$this->errorCode = $errorCode; |
40
|
|
|
} |
41
|
|
|
|
42
|
46 |
|
if ($errorMessage !== '') { |
43
|
1 |
|
$this->errorMessage = $errorMessage; |
44
|
|
|
} |
45
|
|
|
|
46
|
46 |
|
if (!$this->errorMessage) { |
47
|
3 |
|
$this->errorMessage = (string)$this->result->getResponse()->getBody(); |
48
|
|
|
} |
49
|
|
|
|
50
|
46 |
|
$this->distinguishSignatureAndCredentialError(); |
51
|
|
|
|
52
|
46 |
|
parent::__construct( |
53
|
46 |
|
$this->getParentMessage(), |
54
|
46 |
|
$this->result->getResponse()->getStatusCode() |
55
|
|
|
); |
56
|
46 |
|
} |
57
|
|
|
|
58
|
46 |
|
private function distinguishSignatureAndCredentialError() |
59
|
|
|
{ |
60
|
46 |
|
if ($this->result->getRequest()) { |
61
|
44 |
|
$same = Stringy::create($this->errorMessage)->contains($this->result->getRequest()->stringToBeSigned); |
62
|
|
|
// If the string to be signed are the same with server's, it is considered a credential error. |
63
|
44 |
|
if ($same) { |
64
|
|
|
$this->errorCode = 'InvalidAccessKeySecret'; |
65
|
|
|
$this->errorMessage = 'Specified Access Key Secret is not valid.'; |
66
|
|
|
} |
67
|
|
|
} |
68
|
46 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
46 |
|
private function getParentMessage() |
74
|
|
|
{ |
75
|
46 |
|
$data = $this->result->toArray(); |
76
|
46 |
|
\ksort($data); |
77
|
46 |
|
$message = ''; |
78
|
46 |
|
foreach ($data as $key => $value) { |
79
|
44 |
|
$message .= "$key:$value "; |
80
|
|
|
} |
81
|
46 |
|
return $message; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
46 |
|
private function settingProperties() |
88
|
|
|
{ |
89
|
46 |
|
if (isset($this->result['message'])) { |
90
|
1 |
|
$this->errorMessage = $this->result['message']; |
91
|
1 |
|
$this->errorCode = $this->result['code']; |
92
|
|
|
} |
93
|
46 |
|
if (isset($this->result['Message'])) { |
94
|
43 |
|
$this->errorMessage = $this->result['Message']; |
95
|
43 |
|
$this->errorCode = $this->result['Code']; |
96
|
|
|
} |
97
|
46 |
|
if (isset($this->result['errorMsg'])) { |
98
|
1 |
|
$this->errorMessage = $this->result['errorMsg']; |
99
|
1 |
|
$this->errorCode = $this->result['errorCode']; |
100
|
|
|
} |
101
|
46 |
|
if (isset($this->result['requestId'])) { |
102
|
1 |
|
$this->requestId = $this->result['requestId']; |
103
|
|
|
} |
104
|
46 |
|
if (isset($this->result['RequestId'])) { |
105
|
43 |
|
$this->requestId = $this->result['RequestId']; |
106
|
|
|
} |
107
|
46 |
|
} |
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
|
|
|
|