Skip to content

Commit

Permalink
Handle HTTP-413 response code from Bulk endpoint (#2055)
Browse files Browse the repository at this point in the history
Add throwing `\Elastica\Exception\RequestEntityTooLargeException` on HTTP-413 responses in `\Elastica\Bulk`
  • Loading branch information
Vetaxon authored Mar 29, 2022
1 parent 3a19d82 commit 7723faa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
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()) {
case 413: throw new RequestEntityTooLargeException();
}

$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();
}
}

0 comments on commit 7723faa

Please sign in to comment.