diff --git a/CHANGELOG.md b/CHANGELOG.md index 66732de497..c7232b5f9d 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added * Added explicit return annotation to `Elastica\Multi\ResultSet::current()` and `Elastica\Multi\ResultSet::offsetGet()` by @franmomu [2056](https://github.com/ruflin/Elastica/pull/2056) +* Add throwing `\Elastica\Exception\RequestEntityTooLargeException` on HTTP-413 responses in `\Elastica\Bulk` by @Vetaxon [2055](https://github.com/ruflin/Elastica/pull/2055) ### Changed ### Deprecated diff --git a/src/Bulk.php b/src/Bulk.php index bf5b96916a..0bfa120759 100644 --- a/src/Bulk.php +++ b/src/Bulk.php @@ -9,6 +9,7 @@ use Elastica\Exception\Bulk\ResponseException; use Elastica\Exception\Bulk\ResponseException as BulkResponseException; use Elastica\Exception\InvalidException; +use Elastica\Exception\RequestEntityTooLargeException; use Elastica\Script\AbstractScript; class Bulk @@ -296,6 +297,10 @@ public function send(): ResponseSet */ protected function _processResponse(Response $response): ResponseSet { + switch ($response->getStatus()) { + case 413: throw new RequestEntityTooLargeException(); + } + $responseData = $response->getData(); $actions = $this->getActions(); diff --git a/src/Exception/RequestEntityTooLargeException.php b/src/Exception/RequestEntityTooLargeException.php new file mode 100644 index 0000000000..21829ea144 --- /dev/null +++ b/src/Exception/RequestEntityTooLargeException.php @@ -0,0 +1,13 @@ +setIndex('unittest'); $this->assertTrue($bulk->hasIndex()); } + + /** + * @group unit + */ + public function testSendRequestEntityTooLargeExceptionIf413Response(): void + { + $response = new ElasticaResponse('', 413); + + /** @var Client|MockObject $clientMock */ + $clientMock = $this->createMock(Client::class); + $clientMock + ->method('request') + ->willReturn($response) + ; + + $documents = [ + new Document(1, ['name' => 'Mister Fantastic']), + new Document(2, ['name' => 'Invisible Woman']), + new Document(2, ['name' => 'The Human Torch']), + ]; + + $bulk = new Bulk($clientMock); + $bulk->addDocuments($documents); + + $this->expectException(RequestEntityTooLargeException::class); + $bulk->send(); + } }