ServiceResponseException.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Aws\Common\Exception;
  17. use Guzzle\Http\Message\RequestInterface;
  18. use Guzzle\Http\Message\Response;
  19. /**
  20. * Default AWS exception
  21. */
  22. class ServiceResponseException extends RuntimeException
  23. {
  24. /**
  25. * @var Response Response
  26. */
  27. protected $response;
  28. /**
  29. * @var RequestInterface Request
  30. */
  31. protected $request;
  32. /**
  33. * @var string Request ID
  34. */
  35. protected $requestId;
  36. /**
  37. * @var string Exception type (client / server)
  38. */
  39. protected $exceptionType;
  40. /**
  41. * @var string Exception code
  42. */
  43. protected $exceptionCode;
  44. /**
  45. * Set the exception code
  46. *
  47. * @param string $code Exception code
  48. */
  49. public function setExceptionCode($code)
  50. {
  51. $this->exceptionCode = $code;
  52. }
  53. /**
  54. * Get the exception code
  55. *
  56. * @return string|null
  57. */
  58. public function getExceptionCode()
  59. {
  60. return $this->exceptionCode;
  61. }
  62. /**
  63. * Set the exception type
  64. *
  65. * @param string $type Exception type
  66. */
  67. public function setExceptionType($type)
  68. {
  69. $this->exceptionType = $type;
  70. }
  71. /**
  72. * Get the exception type (one of client or server)
  73. *
  74. * @return string|null
  75. */
  76. public function getExceptionType()
  77. {
  78. return $this->exceptionType;
  79. }
  80. /**
  81. * Set the request ID
  82. *
  83. * @param string $id Request ID
  84. */
  85. public function setRequestId($id)
  86. {
  87. $this->requestId = $id;
  88. }
  89. /**
  90. * Get the Request ID
  91. *
  92. * @return string|null
  93. */
  94. public function getRequestId()
  95. {
  96. return $this->requestId;
  97. }
  98. /**
  99. * Set the associated response
  100. *
  101. * @param Response $response Response
  102. */
  103. public function setResponse(Response $response)
  104. {
  105. $this->response = $response;
  106. }
  107. /**
  108. * Get the associated response object
  109. *
  110. * @return Response|null
  111. */
  112. public function getResponse()
  113. {
  114. return $this->response;
  115. }
  116. /**
  117. * Set the associated request
  118. *
  119. * @param RequestInterface $request
  120. */
  121. public function setRequest(RequestInterface $request)
  122. {
  123. $this->request = $request;
  124. }
  125. /**
  126. * Get the associated request object
  127. *
  128. * @return RequestInterface|null
  129. */
  130. public function getRequest()
  131. {
  132. return $this->request;
  133. }
  134. /**
  135. * Get the status code of the response
  136. *
  137. * @return int|null
  138. */
  139. public function getStatusCode()
  140. {
  141. return $this->response ? $this->response->getStatusCode() : null;
  142. }
  143. /**
  144. * Cast to a string
  145. *
  146. * @return string
  147. */
  148. public function __toString()
  149. {
  150. $message = get_class($this) . ': '
  151. . 'AWS Error Code: ' . $this->getExceptionCode() . ', '
  152. . 'Status Code: ' . $this->getStatusCode() . ', '
  153. . 'AWS Request ID: ' . $this->getRequestId() . ', '
  154. . 'AWS Error Type: ' . $this->getExceptionType() . ', '
  155. . 'AWS Error Message: ' . $this->getMessage();
  156. // Add the User-Agent if available
  157. if ($this->request) {
  158. $message .= ', ' . 'User-Agent: ' . $this->request->getHeader('User-Agent');
  159. }
  160. return $message;
  161. }
  162. }