CommandException.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace GuzzleHttp\Command\Exception;
  3. use GuzzleHttp\Exception\GuzzleException;
  4. use GuzzleHttp\Exception\RequestException;
  5. use GuzzleHttp\Command\CommandInterface;
  6. use Psr\Http\Message\RequestInterface;
  7. use Psr\Http\Message\ResponseInterface;
  8. /**
  9. * Exception encountered while executing a command.
  10. */
  11. class CommandException extends \RuntimeException implements GuzzleException
  12. {
  13. /** @var CommandInterface */
  14. private $command;
  15. /** @var RequestInterface */
  16. private $request;
  17. /** @var ResponseInterface */
  18. private $response;
  19. /**
  20. * @param CommandInterface $command
  21. * @param \Exception $prev
  22. * @return CommandException
  23. */
  24. public static function fromPrevious(CommandInterface $command, \Exception $prev)
  25. {
  26. // If the exception is already a command exception, return it.
  27. if ($prev instanceof self && $command === $prev->getCommand()) {
  28. return $prev;
  29. }
  30. // If the exception is a RequestException, get the Request and Response.
  31. $request = $response = null;
  32. if ($prev instanceof RequestException) {
  33. $request = $prev->getRequest();
  34. $response = $prev->getResponse();
  35. }
  36. // Throw a more specific exception for 4XX or 5XX responses.
  37. $class = self::class;
  38. $statusCode = $response ? $response->getStatusCode() : 0;
  39. if ($statusCode >= 400 && $statusCode < 500) {
  40. $class = CommandClientException::class;
  41. } elseif ($statusCode >= 500 && $statusCode < 600) {
  42. $class = CommandServerException::class;
  43. }
  44. // Prepare the message.
  45. $message = 'There was an error executing the ' . $command->getName()
  46. . ' command: ' . $prev->getMessage();
  47. // Create the exception.
  48. return new $class($message, $command, $prev, $request, $response);
  49. }
  50. /**
  51. * @param string $message Exception message
  52. * @param CommandInterface $command
  53. * @param \Exception $previous Previous exception (if any)
  54. * @param RequestInterface $request
  55. * @param ResponseInterface $response
  56. */
  57. public function __construct(
  58. $message,
  59. CommandInterface $command,
  60. \Exception $previous = null,
  61. RequestInterface $request = null,
  62. ResponseInterface $response = null
  63. ) {
  64. $this->command = $command;
  65. $this->request = $request;
  66. $this->response = $response;
  67. parent::__construct($message, 0, $previous);
  68. }
  69. /**
  70. * Gets the command that failed.
  71. *
  72. * @return CommandInterface
  73. */
  74. public function getCommand()
  75. {
  76. return $this->command;
  77. }
  78. /**
  79. * Gets the request that caused the exception
  80. *
  81. * @return RequestInterface|null
  82. */
  83. public function getRequest()
  84. {
  85. return $this->request;
  86. }
  87. /**
  88. * Gets the associated response
  89. *
  90. * @return ResponseInterface|null
  91. */
  92. public function getResponse()
  93. {
  94. return $this->response;
  95. }
  96. }