1010class 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
0 commit comments