ErrorChunk.php 3.2 KB

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