RejectionException.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Promise;
  4. /**
  5. * A special exception that is thrown when waiting on a rejected promise.
  6. *
  7. * The reason value is available via the getReason() method.
  8. */
  9. class RejectionException extends \RuntimeException
  10. {
  11. /** @var mixed Rejection reason. */
  12. private $reason;
  13. /**
  14. * @param mixed $reason Rejection reason.
  15. * @param string|null $description Optional description.
  16. */
  17. public function __construct($reason, string $description = null)
  18. {
  19. $this->reason = $reason;
  20. $message = 'The promise was rejected';
  21. if ($description) {
  22. $message .= ' with reason: '.$description;
  23. } elseif (is_string($reason)
  24. || (is_object($reason) && method_exists($reason, '__toString'))
  25. ) {
  26. $message .= ' with reason: '.$this->reason;
  27. } elseif ($reason instanceof \JsonSerializable) {
  28. $message .= ' with reason: '.json_encode($this->reason, JSON_PRETTY_PRINT);
  29. }
  30. parent::__construct($message);
  31. }
  32. /**
  33. * Returns the rejection reason.
  34. *
  35. * @return mixed
  36. */
  37. public function getReason()
  38. {
  39. return $this->reason;
  40. }
  41. }