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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +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`
### Changed
### Deprecated
### Removed
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
16 changes: 16 additions & 0 deletions src/Exception/RequestEntityTooLargeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Elastica\Exception;

use Throwable;

class RequestEntityTooLargeException extends \RuntimeException implements ExceptionInterface
{
/**
* @param Throwable|null $previous
*/
thePanz marked this conversation as resolved.
Show resolved Hide resolved
public function __construct(Throwable $previous = null)
{
parent::__construct('Request entity is too large.', 0, $previous);
}
}
32 changes: 32 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,32 @@ 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);

$index = $this->_createIndex();
thePanz marked this conversation as resolved.
Show resolved Hide resolved

$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();
}
}
21 changes: 21 additions & 0 deletions tests/Exception/RequestEntityTooLargeExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Elastica\Test\Exception;

use Elastica\Exception\RequestEntityTooLargeException;

/**
* @internal
*/
class RequestEntityTooLargeExceptionTest extends AbstractExceptionTest
thePanz marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @group unit
*/
public function testInstanceDefaultMessage(): void
{
$message = 'Request entity is too large.';
$exception = new RequestEntityTooLargeException();
$this->assertEquals($message, $exception->getMessage());
}
}