AwsException.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace Aws\Exception;
  3. use Psr\Http\Message\ResponseInterface;
  4. use Psr\Http\Message\RequestInterface;
  5. use Aws\CommandInterface;
  6. use Aws\ResultInterface;
  7. /**
  8. * Represents an AWS exception that is thrown when a command fails.
  9. */
  10. class AwsException extends \RuntimeException
  11. {
  12. /** @var ResponseInterface */
  13. private $response;
  14. private $request;
  15. private $result;
  16. private $command;
  17. private $requestId;
  18. private $errorType;
  19. private $errorCode;
  20. private $connectionError;
  21. private $transferInfo;
  22. private $errorMessage;
  23. /**
  24. * @param string $message Exception message
  25. * @param CommandInterface $command
  26. * @param array $context Exception context
  27. * @param \Exception $previous Previous exception (if any)
  28. */
  29. public function __construct(
  30. $message,
  31. CommandInterface $command,
  32. array $context = [],
  33. \Exception $previous = null
  34. ) {
  35. $this->command = $command;
  36. $this->response = isset($context['response']) ? $context['response'] : null;
  37. $this->request = isset($context['request']) ? $context['request'] : null;
  38. $this->requestId = isset($context['request_id'])
  39. ? $context['request_id']
  40. : null;
  41. $this->errorType = isset($context['type']) ? $context['type'] : null;
  42. $this->errorCode = isset($context['code']) ? $context['code'] : null;
  43. $this->connectionError = !empty($context['connection_error']);
  44. $this->result = isset($context['result']) ? $context['result'] : null;
  45. $this->transferInfo = isset($context['transfer_stats'])
  46. ? $context['transfer_stats']
  47. : [];
  48. $this->errorMessage = isset($context['message'])
  49. ? $context['message']
  50. : null;
  51. parent::__construct($message, 0, $previous);
  52. }
  53. public function __toString()
  54. {
  55. if (!$this->getPrevious()) {
  56. return parent::__toString();
  57. }
  58. // PHP strangely shows the innermost exception first before the outer
  59. // exception message. It also has a default character limit for
  60. // exception message strings such that the "next" exception (this one)
  61. // might not even get shown, causing developers to attempt to catch
  62. // the inner exception instead of the actual exception because they
  63. // can't see the outer exception's __toString output.
  64. return sprintf(
  65. "exception '%s' with message '%s'\n\n%s",
  66. get_class($this),
  67. $this->getMessage(),
  68. parent::__toString()
  69. );
  70. }
  71. /**
  72. * Get the command that was executed.
  73. *
  74. * @return CommandInterface
  75. */
  76. public function getCommand()
  77. {
  78. return $this->command;
  79. }
  80. /**
  81. * Get the concise error message if any.
  82. *
  83. * @return string|null
  84. */
  85. public function getAwsErrorMessage()
  86. {
  87. return $this->errorMessage;
  88. }
  89. /**
  90. * Get the sent HTTP request if any.
  91. *
  92. * @return RequestInterface|null
  93. */
  94. public function getRequest()
  95. {
  96. return $this->request;
  97. }
  98. /**
  99. * Get the received HTTP response if any.
  100. *
  101. * @return ResponseInterface|null
  102. */
  103. public function getResponse()
  104. {
  105. return $this->response;
  106. }
  107. /**
  108. * Get the result of the exception if available
  109. *
  110. * @return ResultInterface|null
  111. */
  112. public function getResult()
  113. {
  114. return $this->result;
  115. }
  116. /**
  117. * Returns true if this is a connection error.
  118. *
  119. * @return bool
  120. */
  121. public function isConnectionError()
  122. {
  123. return $this->connectionError;
  124. }
  125. /**
  126. * If available, gets the HTTP status code of the corresponding response
  127. *
  128. * @return int|null
  129. */
  130. public function getStatusCode()
  131. {
  132. return $this->response ? $this->response->getStatusCode() : null;
  133. }
  134. /**
  135. * Get the request ID of the error. This value is only present if a
  136. * response was received and is not present in the event of a networking
  137. * error.
  138. *
  139. * @return string|null Returns null if no response was received
  140. */
  141. public function getAwsRequestId()
  142. {
  143. return $this->requestId;
  144. }
  145. /**
  146. * Get the AWS error type.
  147. *
  148. * @return string|null Returns null if no response was received
  149. */
  150. public function getAwsErrorType()
  151. {
  152. return $this->errorType;
  153. }
  154. /**
  155. * Get the AWS error code.
  156. *
  157. * @return string|null Returns null if no response was received
  158. */
  159. public function getAwsErrorCode()
  160. {
  161. return $this->errorCode;
  162. }
  163. /**
  164. * Get all transfer information as an associative array if no $name
  165. * argument is supplied, or gets a specific transfer statistic if
  166. * a $name attribute is supplied (e.g., 'retries_attempted').
  167. *
  168. * @param string $name Name of the transfer stat to retrieve
  169. *
  170. * @return mixed|null|array
  171. */
  172. public function getTransferInfo($name = null)
  173. {
  174. if (!$name) {
  175. return $this->transferInfo;
  176. }
  177. return isset($this->transferInfo[$name])
  178. ? $this->transferInfo[$name]
  179. : null;
  180. }
  181. /**
  182. * Replace the transfer information associated with an exception.
  183. *
  184. * @param array $info
  185. */
  186. public function setTransferInfo(array $info)
  187. {
  188. $this->transferInfo = $info;
  189. }
  190. }