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