ErrorChunk.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 bool $didThrow = false;
  22. private int $offset;
  23. private string $errorMessage;
  24. private ?\Throwable $error = null;
  25. public function __construct(int $offset, \Throwable|string $error)
  26. {
  27. $this->offset = $offset;
  28. if (\is_string($error)) {
  29. $this->errorMessage = $error;
  30. } else {
  31. $this->error = $error;
  32. $this->errorMessage = $error->getMessage();
  33. }
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function isTimeout(): bool
  39. {
  40. $this->didThrow = true;
  41. if (null !== $this->error) {
  42. throw new TransportException($this->errorMessage, 0, $this->error);
  43. }
  44. return true;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function isFirst(): bool
  50. {
  51. $this->didThrow = true;
  52. throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function isLast(): bool
  58. {
  59. $this->didThrow = true;
  60. throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getInformationalStatus(): ?array
  66. {
  67. $this->didThrow = true;
  68. throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getContent(): string
  74. {
  75. $this->didThrow = true;
  76. throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getOffset(): int
  82. {
  83. return $this->offset;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getError(): ?string
  89. {
  90. return $this->errorMessage;
  91. }
  92. public function didThrow(bool $didThrow = null): bool
  93. {
  94. if (null !== $didThrow && $this->didThrow !== $didThrow) {
  95. return !$this->didThrow = $didThrow;
  96. }
  97. return $this->didThrow;
  98. }
  99. public function __sleep(): array
  100. {
  101. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  102. }
  103. public function __wakeup()
  104. {
  105. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  106. }
  107. public function __destruct()
  108. {
  109. if (!$this->didThrow) {
  110. $this->didThrow = true;
  111. throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
  112. }
  113. }
  114. }