From 071ddd4ad437e4db9eef56ac35efde742b3d73b3 Mon Sep 17 00:00:00 2001 From: Vitalii Ivanov Date: Tue, 15 Feb 2022 14:48:27 +0200 Subject: [PATCH 1/8] - introduced RequestEntityTooLargeException - adjusted Bulk, throw RequestEntityTooLargeException if response code 413 --- src/Bulk.php | 5 +++++ src/Exception/RequestEntityTooLargeException.php | 11 +++++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/Exception/RequestEntityTooLargeException.php 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..c522f14c01 --- /dev/null +++ b/src/Exception/RequestEntityTooLargeException.php @@ -0,0 +1,11 @@ + Date: Mon, 21 Mar 2022 09:30:26 +0200 Subject: [PATCH 2/8] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c87d0cdf09..ce22973499 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1959,3 +1959,7 @@ The changelog before version 2.0.0 was organised by date. All changes can be fou 2011-03-21 - ChildrenAggregation added - https://www.elastic.co/guide/en/elasticsearch/guide/current/children-agg.html + +2022-03-21 +- Added `\Elastica\Exception\RequestEntityTooLargeException` +- Adjusted `\Elastica\Bulk`, throw RequestEntityTooLargeException if response code 413 From 696041c87db50a0f0146e1e0a699aff17dc70403 Mon Sep 17 00:00:00 2001 From: Vitalii Ivanov <38527025+Vetaxon@users.noreply.github.com> Date: Tue, 22 Mar 2022 11:31:17 +0200 Subject: [PATCH 3/8] Moved change log to unreleased block --- CHANGELOG.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce22973499..4c769eff3d 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 support for `symfony/deprecation-contracts` 3.0 by @rguennichi [#2047](https://github.com/ruflin/Elastica/pull/2047) * Added aggregation `auto_date_histogram` @andriinavrotskii [#2051](https://github.com/ruflin/Elastica/pull/2051) +* Add throwing `\Elastica\Exception\RequestEntityTooLargeException` on HTTP-413 responses in `\Elastica\Bulk` ### Changed ### Deprecated ### Removed @@ -1959,7 +1960,3 @@ The changelog before version 2.0.0 was organised by date. All changes can be fou 2011-03-21 - ChildrenAggregation added - https://www.elastic.co/guide/en/elasticsearch/guide/current/children-agg.html - -2022-03-21 -- Added `\Elastica\Exception\RequestEntityTooLargeException` -- Adjusted `\Elastica\Bulk`, throw RequestEntityTooLargeException if response code 413 From c51e70da0ba57584f5c7d1a6008c0136c2b38e9f Mon Sep 17 00:00:00 2001 From: Vitalii Ivanov Date: Tue, 29 Mar 2022 11:40:06 +0300 Subject: [PATCH 4/8] added RequestEntityTooLargeExceptionTest added test BulkTest to check RequestEntityTooLargeExceptionTest --- tests/BulkTest.php | 31 +++++++++++++++++++ .../RequestEntityTooLargeExceptionTest.php | 21 +++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 tests/Exception/RequestEntityTooLargeExceptionTest.php diff --git a/tests/BulkTest.php b/tests/BulkTest.php index ef1dbdfc97..12a6cf4bf7 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -9,10 +9,13 @@ use Elastica\Bulk\Action\IndexDocument; use Elastica\Bulk\Action\UpdateDocument; use Elastica\Bulk\Response; +use Elastica\Client; use Elastica\Document; use Elastica\Exception\Bulk\ResponseException; use Elastica\Exception\InvalidException; use Elastica\Exception\NotFoundException; +use Elastica\Exception\RequestEntityTooLargeException; +use Elastica\Response as ElasticaResponse; use Elastica\Script\Script; use Elastica\Test\Base as BaseTest; @@ -765,4 +768,32 @@ public function testHasIndex(): void $bulk->setIndex('unittest'); $this->assertTrue($bulk->hasIndex()); } + + /** + * @group unit + */ + public function testSendRequestEntityTooLargeExceptionIf413Response(): void + { + $response = new ElasticaResponse('', 413); + + /** @var Client|\PHPUnit\Framework\MockObject\MockObject $clientMock */ + $clientMock = $this->createMock(Client::class); + $clientMock + ->method('request') + ->willReturn($response); + + $index = $this->_createIndex(); + + $documents = [ + new Document(1, ['name' => 'Mister Fantastic'], $index), + new Document(2, ['name' => 'Invisible Woman'], $index), + new Document(2, ['name' => 'The Human Torch'], $index), + ]; + + $bulk = new Bulk($clientMock); + $bulk->addDocuments($documents); + + $this->expectException(RequestEntityTooLargeException::class); + $bulk->send(); + } } diff --git a/tests/Exception/RequestEntityTooLargeExceptionTest.php b/tests/Exception/RequestEntityTooLargeExceptionTest.php new file mode 100644 index 0000000000..e18745ba7a --- /dev/null +++ b/tests/Exception/RequestEntityTooLargeExceptionTest.php @@ -0,0 +1,21 @@ +assertEquals($message, $exception->getMessage()); + } +} From 07eae0d98ffb3a2d1b289f5081e93c3c3c9d465e Mon Sep 17 00:00:00 2001 From: Vitalii Ivanov Date: Tue, 29 Mar 2022 16:42:40 +0300 Subject: [PATCH 5/8] fixed constructor of RequestEntityTooLargeException --- src/Exception/RequestEntityTooLargeException.php | 9 +++++++-- tests/BulkTest.php | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Exception/RequestEntityTooLargeException.php b/src/Exception/RequestEntityTooLargeException.php index c522f14c01..c7c79f9c05 100644 --- a/src/Exception/RequestEntityTooLargeException.php +++ b/src/Exception/RequestEntityTooLargeException.php @@ -2,10 +2,15 @@ namespace Elastica\Exception; +use Throwable; + class RequestEntityTooLargeException extends \RuntimeException implements ExceptionInterface { /** - * @var string + * @param Throwable|null $previous */ - protected $message = 'Request entity is too large.'; + public function __construct(Throwable $previous = null) + { + parent::__construct('Request entity is too large.', 0, $previous); + } } diff --git a/tests/BulkTest.php b/tests/BulkTest.php index 12a6cf4bf7..d51c5fb9a3 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -18,6 +18,7 @@ use Elastica\Response as ElasticaResponse; use Elastica\Script\Script; use Elastica\Test\Base as BaseTest; +use PHPUnit\Framework\MockObject\MockObject; /** * @internal @@ -776,7 +777,7 @@ public function testSendRequestEntityTooLargeExceptionIf413Response(): void { $response = new ElasticaResponse('', 413); - /** @var Client|\PHPUnit\Framework\MockObject\MockObject $clientMock */ + /** @var Client|MockObject $clientMock */ $clientMock = $this->createMock(Client::class); $clientMock ->method('request') From f8a851ad3602da4c3b7a653a4c9646d9671da920 Mon Sep 17 00:00:00 2001 From: Vitalii Ivanov Date: Tue, 29 Mar 2022 17:09:42 +0300 Subject: [PATCH 6/8] fixed by comments --- .../RequestEntityTooLargeException.php | 3 --- .../RequestEntityTooLargeExceptionTest.php | 21 ------------------- 2 files changed, 24 deletions(-) delete mode 100644 tests/Exception/RequestEntityTooLargeExceptionTest.php diff --git a/src/Exception/RequestEntityTooLargeException.php b/src/Exception/RequestEntityTooLargeException.php index c7c79f9c05..a70b560105 100644 --- a/src/Exception/RequestEntityTooLargeException.php +++ b/src/Exception/RequestEntityTooLargeException.php @@ -6,9 +6,6 @@ class RequestEntityTooLargeException extends \RuntimeException implements ExceptionInterface { - /** - * @param Throwable|null $previous - */ public function __construct(Throwable $previous = null) { parent::__construct('Request entity is too large.', 0, $previous); diff --git a/tests/Exception/RequestEntityTooLargeExceptionTest.php b/tests/Exception/RequestEntityTooLargeExceptionTest.php deleted file mode 100644 index e18745ba7a..0000000000 --- a/tests/Exception/RequestEntityTooLargeExceptionTest.php +++ /dev/null @@ -1,21 +0,0 @@ -assertEquals($message, $exception->getMessage()); - } -} From 3015cdffecd89657edd1672eff8b17f16cd5533f Mon Sep 17 00:00:00 2001 From: Vitalii Ivanov Date: Tue, 29 Mar 2022 17:54:53 +0300 Subject: [PATCH 7/8] make sniffer happy --- src/Exception/RequestEntityTooLargeException.php | 2 +- tests/BulkTest.php | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Exception/RequestEntityTooLargeException.php b/src/Exception/RequestEntityTooLargeException.php index a70b560105..21829ea144 100644 --- a/src/Exception/RequestEntityTooLargeException.php +++ b/src/Exception/RequestEntityTooLargeException.php @@ -6,7 +6,7 @@ class RequestEntityTooLargeException extends \RuntimeException implements ExceptionInterface { - public function __construct(Throwable $previous = null) + public function __construct(?Throwable $previous = null) { parent::__construct('Request entity is too large.', 0, $previous); } diff --git a/tests/BulkTest.php b/tests/BulkTest.php index d51c5fb9a3..1a13274005 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -781,14 +781,13 @@ public function testSendRequestEntityTooLargeExceptionIf413Response(): void $clientMock = $this->createMock(Client::class); $clientMock ->method('request') - ->willReturn($response); - - $index = $this->_createIndex(); + ->willReturn($response) + ; $documents = [ - new Document(1, ['name' => 'Mister Fantastic'], $index), - new Document(2, ['name' => 'Invisible Woman'], $index), - new Document(2, ['name' => 'The Human Torch'], $index), + new Document(1, ['name' => 'Mister Fantastic']), + new Document(2, ['name' => 'Invisible Woman']), + new Document(2, ['name' => 'The Human Torch']), ]; $bulk = new Bulk($clientMock); From bceb11d0aa63dd396e7c57b25d51e5b83e264697 Mon Sep 17 00:00:00 2001 From: Emanuele Panzeri <226021+thePanz@users.noreply.github.com> Date: Tue, 29 Mar 2022 17:04:32 +0200 Subject: [PATCH 8/8] Update Changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b22709bd00..c7232b5f9d 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,8 @@ 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` +* Add throwing `\Elastica\Exception\RequestEntityTooLargeException` on HTTP-413 responses in `\Elastica\Bulk` by @Vetaxon [2055](https://github.com/ruflin/Elastica/pull/2055) + ### Changed ### Deprecated ### Removed