Skip to content

Commit 785bcb8

Browse files
committed
Add bulk request exception static constructor
1 parent 6d224ae commit 785bcb8

4 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/Documents/DocumentManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function index(
4949
$response = $this->client->bulk($params);
5050

5151
if ($response['errors']) {
52-
throw new BulkRequestException($response);
52+
throw BulkRequestException::fromResponse($response);
5353
}
5454

5555
return $this;
@@ -87,7 +87,7 @@ public function delete(
8787
$response = $this->client->bulk($params);
8888

8989
if ($response['errors']) {
90-
throw new BulkRequestException($response);
90+
throw BulkRequestException::fromResponse($response);
9191
}
9292

9393
return $this;

src/Exceptions/BulkRequestException.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,26 @@
66

77
class BulkRequestException extends ErrorException
88
{
9+
/**
10+
* Create a new bulk request exception from an OpenSearch response.
11+
*
12+
* @param array<string, mixed> $response
13+
*/
14+
public static function fromResponse(array $response): self
15+
{
16+
return new self($response, self::makeMessageFromResponse($response));
17+
}
18+
919
/**
1020
* Create a new bulk request exception instance.
1121
*
1222
* @param array<string, mixed> $response
1323
*/
1424
public function __construct(
1525
protected array $response,
26+
string $message = '',
1627
) {
17-
parent::__construct($this->makeErrorFromResponse());
28+
parent::__construct($message);
1829
}
1930

2031
/**
@@ -42,9 +53,9 @@ public function getResponse(): array
4253
/**
4354
* Create the exception message from the OpenSearch bulk response.
4455
*/
45-
protected function makeErrorFromResponse(): string
56+
protected static function makeMessageFromResponse(array $response): string
4657
{
47-
$items = $this->response['items'] ?? [];
58+
$items = $response['items'] ?? [];
4859
$count = count($items);
4960

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

src/Indices/Mapping.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public function __call(string $method, array $parameters): self
150150
public function toArray(): array
151151
{
152152
$mapping = [];
153+
153154
$properties = $this->properties->toArray();
154155

155156
if (isset($this->isFieldNamesEnabled)) {

tests/Unit/Exceptions/BulkRequestExceptionTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
],
2828
];
2929

30-
$exception = new BulkRequestException($response);
30+
$exception = BulkRequestException::fromResponse($response);
3131

3232
$this->assertSame($response, $exception->getResponse());
3333
});
@@ -55,7 +55,7 @@
5555
],
5656
];
5757

58-
$exception = new BulkRequestException($response);
58+
$exception = BulkRequestException::fromResponse($response);
5959

6060
$this->assertEquals(
6161
'1 bulk operation(s) did not complete successfully. Error: document_missing_exception. Reason: [_doc][5]: document missing. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
@@ -101,7 +101,7 @@
101101
],
102102
];
103103

104-
$exception = new BulkRequestException($response);
104+
$exception = BulkRequestException::fromResponse($response);
105105

106106
$this->assertEquals(
107107
'2 bulk operation(s) did not complete successfully. First error: document_missing_exception. Reason: [_doc][5]: document missing. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
@@ -125,7 +125,7 @@
125125
],
126126
];
127127

128-
$exception = new BulkRequestException($response);
128+
$exception = BulkRequestException::fromResponse($response);
129129

130130
$this->assertEquals(
131131
'1 bulk operation(s) did not complete successfully. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',
@@ -140,7 +140,7 @@
140140
'items' => [],
141141
];
142142

143-
$exception = new BulkRequestException($response);
143+
$exception = BulkRequestException::fromResponse($response);
144144

145145
$this->assertEquals(
146146
'One or more did not complete successfully. Catch the exception and use the DirectoryTree\OpenSearchAdapter\Exceptions\BulkRequestException::getResponse() method to get more details.',

0 commit comments

Comments
 (0)