Blame view

vendor/alibabacloud/client/src/Exception/ServerException.php 3.94 KB
f0d6cde5   xu   vendor
1
2
3
4
<?php

namespace AlibabaCloud\Client\Exception;

f0d6cde5   xu   vendor
5
use Stringy\Stringy;
0c34aba8   xu   更新阿里云短信接口
6
7
8
use RuntimeException;
use AlibabaCloud\Client\SDK;
use AlibabaCloud\Client\Result\Result;
f0d6cde5   xu   vendor
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

/**
 * 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(),
0c34aba8   xu   更新阿里云短信接口
49
            $this->result->getStatusCode()
f0d6cde5   xu   vendor
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
        );
    }

    /**
     * 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()
0c34aba8   xu   更新阿里云短信接口
86
            && Stringy::create($this->errorMessage)->contains($this->result->getRequest()->stringToSign())) {
f0d6cde5   xu   vendor
87
88
89
90
91
92
93
94
95
96
97
            $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()
    {
0c34aba8   xu   更新阿里云短信接口
98
        $body = (string)$this->result->getBody();
f0d6cde5   xu   vendor
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        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\"";
0c34aba8   xu   更新阿里云短信接口
117
118
            if ($this->result) {
                $message .= ' ' . $this->result->getStatusCode();
f0d6cde5   xu   vendor
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
            }
        }

        return $message;
    }

    /**
     * @return Result
     */
    public function getResult()
    {
        return $this->result;
    }

    /**
f0d6cde5   xu   vendor
134
     * @return string
f0d6cde5   xu   vendor
135
     */
0c34aba8   xu   更新阿里云短信接口
136
    public function getRequestId()
f0d6cde5   xu   vendor
137
    {
0c34aba8   xu   更新阿里云短信接口
138
        return $this->requestId;
f0d6cde5   xu   vendor
139
140
141
    }

    /**
0c34aba8   xu   更新阿里云短信接口
142
143
     * @codeCoverageIgnore
     * @deprecated
f0d6cde5   xu   vendor
144
     */
0c34aba8   xu   更新阿里云短信接口
145
    public function getErrorType()
f0d6cde5   xu   vendor
146
    {
0c34aba8   xu   更新阿里云短信接口
147
        return 'Server';
f0d6cde5   xu   vendor
148
149
150
151
    }

    /**
     * @codeCoverageIgnore
0c34aba8   xu   更新阿里云短信接口
152
     * @deprecated
f0d6cde5   xu   vendor
153
154
155
     */
    public function getHttpStatus()
    {
0c34aba8   xu   更新阿里云短信接口
156
        return $this->getResult()->getStatusCode();
f0d6cde5   xu   vendor
157
158
    }
}