ServiceResponseException.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace Qcloud\Cos\Exception;
  3. use Psr\Http\Message\RequestInterface;
  4. use Psr\Http\Message\ResponseInterface;
  5. class ServiceResponseException extends \RuntimeException {
  6. /**
  7. * @var Response Response
  8. */
  9. protected $response;
  10. /**
  11. * @var RequestInterface Request
  12. */
  13. protected $request;
  14. /**
  15. * @var string Request ID
  16. */
  17. protected $requestId;
  18. /**
  19. * @var string Exception type (client / server)
  20. */
  21. protected $exceptionType;
  22. /**
  23. * @var string Exception code
  24. */
  25. protected $exceptionCode;
  26. /**
  27. * Set the exception code
  28. *
  29. * @param string $code Exception code
  30. */
  31. public function setExceptionCode($code) {
  32. $this->exceptionCode = $code;
  33. }
  34. /**
  35. * Get the exception code
  36. *
  37. * @return string|null
  38. */
  39. public function getExceptionCode() {
  40. return $this->exceptionCode;
  41. }
  42. /**
  43. * Set the exception type
  44. *
  45. * @param string $type Exception type
  46. */
  47. public function setExceptionType($type) {
  48. $this->exceptionType = $type;
  49. }
  50. /**
  51. * Get the exception type (one of client or server)
  52. *
  53. * @return string|null
  54. */
  55. public function getExceptionType() {
  56. return $this->exceptionType;
  57. }
  58. /**
  59. * Set the request ID
  60. *
  61. * @param string $id Request ID
  62. */
  63. public function setRequestId($id) {
  64. $this->requestId = $id;
  65. }
  66. /**
  67. * Get the Request ID
  68. *
  69. * @return string|null
  70. */
  71. public function getRequestId() {
  72. return $this->requestId;
  73. }
  74. /**
  75. * Set the associated response
  76. *
  77. * @param Response $response
  78. */
  79. public function setResponse(ResponseInterface $response) {
  80. $this->response = $response;
  81. }
  82. /**
  83. * Get the associated response object
  84. *
  85. * @return Response|null
  86. */
  87. public function getResponse() {
  88. return $this->response;
  89. }
  90. /**
  91. * Set the associated request
  92. *
  93. * @param RequestInterface $request
  94. */
  95. public function setRequest(RequestInterface $request) {
  96. $this->request = $request;
  97. }
  98. /**
  99. * Get the associated request object
  100. *
  101. * @return RequestInterface|null
  102. */
  103. public function getRequest() {
  104. return $this->request;
  105. }
  106. /**
  107. * Get the status code of the response
  108. *
  109. * @return int|null
  110. */
  111. public function getStatusCode() {
  112. return $this->response ? $this->response->getStatusCode() : null;
  113. }
  114. /**
  115. * Cast to a string
  116. *
  117. * @return string
  118. */
  119. public function __toString() {
  120. $message = get_class($this) . ': '
  121. . 'Cos Error Code: ' . $this->getExceptionCode() . ', '
  122. . 'Status Code: ' . $this->getStatusCode() . ', '
  123. . 'Cos Request ID: ' . $this->getRequestId() . ', '
  124. . 'Cos Error Type: ' . $this->getExceptionType() . ', '
  125. . 'Cos Error Message: ' . $this->getMessage();
  126. // Add the User-Agent if available
  127. if ($this->request) {
  128. $message .= ', ' . 'User-Agent: ' . $this->request->getHeader('User-Agent')[0];
  129. }
  130. return $message;
  131. }
  132. /**
  133. * Get the request ID of the error. This value is only present if a
  134. * response was received, and is not present in the event of a networking
  135. * error.
  136. *
  137. * Same as `getRequestId()` method, but matches the interface for SDKv3.
  138. *
  139. * @return string|null Returns null if no response was received
  140. */
  141. public function getCosRequestId() {
  142. return $this->requestId;
  143. }
  144. /**
  145. * Get the Cos error type.
  146. *
  147. * Same as `getExceptionType()` method, but matches the interface for SDKv3.
  148. *
  149. * @return string|null Returns null if no response was received
  150. */
  151. public function getCosErrorType() {
  152. return $this->exceptionType;
  153. }
  154. /**
  155. * Get the Cos error code.
  156. *
  157. * Same as `getExceptionCode()` method, but matches the interface for SDKv3.
  158. *
  159. * @return string|null Returns null if no response was received
  160. */
  161. public function getCosErrorCode() {
  162. return $this->exceptionCode;
  163. }
  164. }