Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve https://github.com/ruflin/Elastica/issues/1737: handle 413 response #2055

Merged
merged 9 commits into from
Mar 29, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -296,6 +297,10 @@ public function send(): ResponseSet
*/
protected function _processResponse(Response $response): ResponseSet
{
switch ($response->getStatus()) {
thePanz marked this conversation as resolved.
Show resolved Hide resolved
case 413: throw new RequestEntityTooLargeException();
thePanz marked this conversation as resolved.
Show resolved Hide resolved
}

$responseData = $response->getData();

$actions = $this->getActions();
Expand Down
13 changes: 13 additions & 0 deletions src/Exception/RequestEntityTooLargeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Elastica\Exception;

use Throwable;

class RequestEntityTooLargeException extends \RuntimeException implements ExceptionInterface
{
public function __construct(?Throwable $previous = null)
{
parent::__construct('Request entity is too large.', 0, $previous);
}
}
31 changes: 31 additions & 0 deletions tests/BulkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
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;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @internal
Expand Down Expand Up @@ -765,4 +769,31 @@ public function testHasIndex(): void
$bulk->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();
}
}