ErrorChunk.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpClient\Chunk;
  11. use Symfony\Component\HttpClient\Exception\TimeoutException;
  12. use Symfony\Component\HttpClient\Exception\TransportException;
  13. use Symfony\Contracts\HttpClient\ChunkInterface;
  14. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. *
  17. * @internal
  18. */
  19. class ErrorChunk implements ChunkInterface
  20. {
  21. private $didThrow = false;
  22. private $offset;
  23. private $errorMessage;
  24. private $error;
  25. /**
  26. * @param \Throwable|string $error
  27. */
  28. public function __construct(int $offset, $error)
  29. {
  30. $this->offset = $offset;
  31. if (\is_string($error)) {
  32. $this->errorMessage = $error;
  33. } else {
  34. $this->error = $error;
  35. $this->errorMessage = $error->getMessage();
  36. }
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function isTimeout(): bool
  42. {
  43. $this->didThrow = true;
  44. if (null !== $this->error) {
  45. throw new TransportException($this->errorMessage, 0, $this->error);
  46. }
  47. return true;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function isFirst(): bool
  53. {
  54. $this->didThrow = true;
  55. throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function isLast(): bool
  61. {
  62. $this->didThrow = true;
  63. throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getInformationalStatus(): ?array
  69. {
  70. $this->didThrow = true;
  71. throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getContent(): string
  77. {
  78. $this->didThrow = true;
  79. throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getOffset(): int
  85. {
  86. return $this->offset;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getError(): ?string
  92. {
  93. return $this->errorMessage;
  94. }
  95. /**
  96. * @return bool Whether the wrapped error has been thrown or not
  97. */
  98. public function didThrow(bool $didThrow = null): bool
  99. {
  100. if (null !== $didThrow && $this->didThrow !== $didThrow) {
  101. return !$this->didThrow = $didThrow;
  102. }
  103. return $this->didThrow;
  104. }
  105. public function __sleep()
  106. {
  107. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  108. }
  109. public function __wakeup()
  110. {
  111. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  112. }
  113. public function __destruct()
  114. {
  115. if (!$this->didThrow) {
  116. $this->didThrow = true;
  117. throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
  118. }
  119. }
  120. }