Skip to content

Commit ee29215

Browse files
committed
Refine bulk exception response access
1 parent 7e97bfe commit ee29215

3 files changed

Lines changed: 20 additions & 23 deletions

File tree

src/Exceptions/BulkRequestException.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,24 @@
1010
class BulkRequestException extends ErrorException
1111
{
1212
/**
13-
* Create a new bulk request exception from an OpenSearch response.
13+
* The OpenSearch bulk response.
1414
*
15-
* @param array<string, mixed> $response
15+
* @var array<string, mixed>
1616
*/
17-
public static function fromResponse(array $response): self
18-
{
19-
return new self($response, self::makeMessageFromResponse($response));
20-
}
17+
protected array $response = [];
2118

2219
/**
23-
* Create a new bulk request exception instance.
20+
* Create a new bulk request exception from an OpenSearch response.
2421
*
2522
* @param array<string, mixed> $response
2623
*/
27-
public function __construct(
28-
protected array $response,
29-
string $message = '',
30-
) {
31-
parent::__construct($message);
24+
public static function fromResponse(array $response): self
25+
{
26+
$instance = new self(self::makeMessageFromResponse($response));
27+
28+
$instance->response = $response;
29+
30+
return $instance;
3231
}
3332

3433
/**
@@ -39,7 +38,7 @@ public function __construct(
3938
public function context(): array
4039
{
4140
return [
42-
'response' => $this->getResponse(),
41+
'response' => $this->response(),
4342
];
4443
}
4544

@@ -48,7 +47,7 @@ public function context(): array
4847
*
4948
* @return array<string, mixed>
5049
*/
51-
public function getResponse(): array
50+
public function response(): array
5251
{
5352
return $this->response;
5453
}
@@ -59,6 +58,7 @@ public function getResponse(): array
5958
protected static function makeMessageFromResponse(array $response): string
6059
{
6160
$items = $response['items'] ?? [];
61+
6262
$count = count($items);
6363

6464
$reason = sprintf('%s did not complete successfully.', $count > 0 ? $count.' bulk operation(s)' : 'One or more');
@@ -67,7 +67,7 @@ protected static function makeMessageFromResponse(array $response): string
6767
$firstOperation = reset($failedOperations);
6868
$firstError = ($firstOperation ?? [])['error'] ?? null;
6969

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

src/Search/SearchResponse.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,10 @@ public function suggestions(): array
8181
{
8282
$suggest = $this->response['suggest'] ?? [];
8383

84-
return array_map(
85-
fn (array $suggestions) => array_map(
86-
fn (array $suggestion) => new Suggestion($suggestion),
87-
$suggestions,
88-
),
89-
$suggest,
90-
);
84+
return array_map(fn (array $suggestions) => array_map(
85+
fn (array $suggestion) => new Suggestion($suggestion),
86+
$suggestions,
87+
), $suggest);
9188
}
9289

9390
/**

tests/Unit/Exceptions/BulkRequestExceptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
$exception = BulkRequestException::fromResponse($response);
3131

32-
$this->assertSame($response, $exception->getResponse());
32+
$this->assertSame($response, $exception->response());
3333
});
3434

3535
test('first error message from response is given in exception message', function () {

0 commit comments

Comments
 (0)