Skip to content

Commit

Permalink
Merge pull request #22 from stevebauman/feature-additional-exception-…
Browse files Browse the repository at this point in the history
…info

Add first error to exception message for clearer debugging
  • Loading branch information
babenkoivan authored Feb 7, 2022
2 parents ec2c34e + facccde commit 9df621a
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/Exceptions/BulkRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,38 @@ final class BulkRequestException extends ErrorException

public function __construct(array $response)
{
parent::__construct(
'One or more bulk operations did not complete successfully. ' .
'Catch the exception and use the BulkRequestException::getResponse() method to get more details.'
);

$this->response = $response;

parent::__construct($this->makeErrorFromResponse());
}

public function context(): array
{
return [
'response' => $this->getResponse(),
];
}

public function getResponse(): array
{
return $this->response;
}

private function makeErrorFromResponse(): string
{
$items = $this->response['items'] ?? [];
$count = count($items);

$reason = sprintf('%s did not complete successfully.', $count > 0 ? $count . ' bulk operation(s)' : 'One or more');

$failedOperations = $items[0] ?? [];
$firstOperation = reset($failedOperations);
$firstError = ($firstOperation ?? [])['error'] ?? null;

if (isset($firstError) && isset($firstError['type']) && isset($firstError['reason'])) {
$reason .= sprintf(' %s: %s. Reason: %s.', $count > 1 ? 'First error' : 'Error', $firstError['type'], $firstError['reason']);
}

return sprintf('%s Catch the exception and use the %s::getResponse() method to get more details.', $reason, self::class);
}
}
120 changes: 120 additions & 0 deletions tests/Unit/Exceptions/BulkRequestExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,124 @@ public function test_response_can_be_retrieved(): void

$this->assertSame($response, $exception->getResponse());
}

public function test_first_error_message_from_response_is_given_in_exception_message(): void
{
$response = [
'took' => 486,
'errors' => true,
'items' => [
[
'update' => [
'_index' => 'index1',
'_type' => '_doc',
'_id' => '5',
'status' => 404,
'error' => [
'type' => 'document_missing_exception',
'reason' => '[_doc][5]: document missing',
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
'shard' => '0',
'index' => 'index1',
],
],
],
],
];

$exception = new BulkRequestException($response);

$this->assertEquals(
'1 bulk operation(s) did not complete successfully. Error: document_missing_exception. Reason: [_doc][5]: document missing. Catch the exception and use the ElasticAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
$exception->getMessage()
);
}

public function test_exception_can_be_throw_with_many_errors_in_response(): void
{
$response = [
'took' => 486,
'errors' => true,
'items' => [
[
'update' => [
'_index' => 'index1',
'_type' => '_doc',
'_id' => '5',
'status' => 404,
'error' => [
'type' => 'document_missing_exception',
'reason' => '[_doc][5]: document missing',
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
'shard' => '0',
'index' => 'index1',
],
],
],
[
'index' => [
'_index' => 'index1',
'_type' => '_doc',
'_id' => '5',
'status' => 404,
'error' => [
'type' => 'mapper_parsing_exception',
'reason' => 'failed to parse field',
'index_uuid' => 'aAsFqTI0Tc2W0LCWgPNrOA',
'shard' => '0',
'index' => 'index1',
],
],
],
],
];

$exception = new BulkRequestException($response);

$this->assertEquals(
'2 bulk operation(s) did not complete successfully. First error: document_missing_exception. Reason: [_doc][5]: document missing. Catch the exception and use the ElasticAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
$exception->getMessage()
);
}

public function test_exception_can_be_throw_with_missing_error_in_response(): void
{
$response = [
'took' => 486,
'errors' => true,
'items' => [
[
'update' => [
'_index' => 'index1',
'_type' => '_doc',
'_id' => '5',
'status' => 404,
],
],
],
];

$exception = new BulkRequestException($response);

$this->assertEquals(
'1 bulk operation(s) did not complete successfully. Catch the exception and use the ElasticAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
$exception->getMessage()
);
}

public function test_exception_can_be_throw_with_missing_items_in_response(): void
{
$response = [
'took' => 486,
'errors' => true,
'items' => [],
];

$exception = new BulkRequestException($response);

$this->assertEquals(
'One or more did not complete successfully. Catch the exception and use the ElasticAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
$exception->getMessage()
);
}
}

0 comments on commit 9df621a

Please sign in to comment.