Ei kuvausta

RequestException.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace GuzzleHttp\Exception;
  3. use Psr\Http\Message\RequestInterface;
  4. use Psr\Http\Message\ResponseInterface;
  5. use GuzzleHttp\Promise\PromiseInterface;
  6. /**
  7. * HTTP Request exception
  8. */
  9. class RequestException extends TransferException
  10. {
  11. /** @var RequestInterface */
  12. private $request;
  13. /** @var ResponseInterface */
  14. private $response;
  15. /** @var array */
  16. private $handlerContext;
  17. public function __construct(
  18. $message,
  19. RequestInterface $request,
  20. ResponseInterface $response = null,
  21. \Exception $previous = null,
  22. array $handlerContext = []
  23. ) {
  24. // Set the code of the exception if the response is set and not future.
  25. $code = $response && !($response instanceof PromiseInterface)
  26. ? $response->getStatusCode()
  27. : 0;
  28. parent::__construct($message, $code, $previous);
  29. $this->request = $request;
  30. $this->response = $response;
  31. $this->handlerContext = $handlerContext;
  32. }
  33. /**
  34. * Wrap non-RequestExceptions with a RequestException
  35. *
  36. * @param RequestInterface $request
  37. * @param \Exception $e
  38. *
  39. * @return RequestException
  40. */
  41. public static function wrapException(RequestInterface $request, \Exception $e)
  42. {
  43. return $e instanceof RequestException
  44. ? $e
  45. : new RequestException($e->getMessage(), $request, null, $e);
  46. }
  47. /**
  48. * Factory method to create a new exception with a normalized error message
  49. *
  50. * @param RequestInterface $request Request
  51. * @param ResponseInterface $response Response received
  52. * @param \Exception $previous Previous exception
  53. * @param array $ctx Optional handler context.
  54. *
  55. * @return self
  56. */
  57. public static function create(
  58. RequestInterface $request,
  59. ResponseInterface $response = null,
  60. \Exception $previous = null,
  61. array $ctx = []
  62. ) {
  63. if (!$response) {
  64. return new self(
  65. 'Error completing request',
  66. $request,
  67. null,
  68. $previous,
  69. $ctx
  70. );
  71. }
  72. $level = floor($response->getStatusCode() / 100);
  73. if ($level == '4') {
  74. $label = 'Client error response';
  75. $className = __NAMESPACE__ . '\\ClientException';
  76. } elseif ($level == '5') {
  77. $label = 'Server error response';
  78. $className = __NAMESPACE__ . '\\ServerException';
  79. } else {
  80. $label = 'Unsuccessful response';
  81. $className = __CLASS__;
  82. }
  83. $message = $label . ' [url] ' . $request->getUri()
  84. . ' [http method] ' . $request->getMethod()
  85. . ' [status code] ' . $response->getStatusCode()
  86. . ' [reason phrase] ' . $response->getReasonPhrase();
  87. return new $className($message, $request, $response, $previous, $ctx);
  88. }
  89. /**
  90. * Get the request that caused the exception
  91. *
  92. * @return RequestInterface
  93. */
  94. public function getRequest()
  95. {
  96. return $this->request;
  97. }
  98. /**
  99. * Get the associated response
  100. *
  101. * @return ResponseInterface|null
  102. */
  103. public function getResponse()
  104. {
  105. return $this->response;
  106. }
  107. /**
  108. * Check if a response was received
  109. *
  110. * @return bool
  111. */
  112. public function hasResponse()
  113. {
  114. return $this->response !== null;
  115. }
  116. /**
  117. * Get contextual information about the error from the underlying handler.
  118. *
  119. * The contents of this array will vary depending on which handler you are
  120. * using. It may also be just an empty array. Relying on this data will
  121. * couple you to a specific handler, but can give more debug information
  122. * when needed.
  123. *
  124. * @return array
  125. */
  126. public function getHandlerContext()
  127. {
  128. return $this->handlerContext;
  129. }
  130. }