ServerException.php 4.0 KB

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