@@ -612,7 +612,6 @@ private function buildCurlCommand(string $method, string $uri, ?string $body): s
612
612
private function process4xxError (array $ request , array $ response , array $ ignore ): ?ElasticsearchException
613
613
{
614
614
$ statusCode = $ response ['status ' ];
615
- $ responseBody = $ response ['body ' ];
616
615
617
616
/**
618
617
* @var \Exception $exception
@@ -622,12 +621,8 @@ private function process4xxError(array $request, array $response, array $ignore)
622
621
if (array_search ($ response ['status ' ], $ ignore ) !== false ) {
623
622
return null ;
624
623
}
625
-
626
- // if responseBody is not string, we convert it so it can be used as Exception message
627
- if (!is_string ($ responseBody )) {
628
- $ responseBody = json_encode ($ responseBody );
629
- }
630
-
624
+
625
+ $ responseBody = $ this ->convertBodyToString ($ response ['body ' ], $ statusCode , $ exception );
631
626
if ($ statusCode === 403 ) {
632
627
$ exception = new Forbidden403Exception ($ responseBody , $ statusCode );
633
628
} elseif ($ statusCode === 404 ) {
@@ -672,14 +667,33 @@ private function process5xxError(array $request, array $response, array $ignore)
672
667
} elseif ($ statusCode === 500 && strpos ($ responseBody , 'NoShardAvailableActionException ' ) !== false ) {
673
668
$ exception = new NoShardAvailableException ($ exception ->getMessage (), $ statusCode , $ exception );
674
669
} else {
675
- $ exception = new ServerErrorResponseException ($ responseBody , $ statusCode );
670
+ $ exception = new ServerErrorResponseException (
671
+ $ this ->convertBodyToString ($ responseBody , $ statusCode , $ exception ),
672
+ $ statusCode
673
+ );
676
674
}
677
675
678
676
$ this ->logRequestFail ($ request , $ response , $ exception );
679
677
680
678
throw $ exception ;
681
679
}
682
680
681
+ private function convertBodyToString ($ body , int $ statusCode , ElasticsearchException $ exception ) : string
682
+ {
683
+ if (empty ($ body )) {
684
+ return sprintf (
685
+ "Unknown %d error from Elasticsearch %s " ,
686
+ $ statusCode ,
687
+ $ exception ->getMessage ()
688
+ );
689
+ }
690
+ // if body is not string, we convert it so it can be used as Exception message
691
+ if (!is_string ($ body )) {
692
+ return json_encode ($ body );
693
+ }
694
+ return $ body ;
695
+ }
696
+
683
697
private function tryDeserialize400Error (array $ response ): ElasticsearchException
684
698
{
685
699
return $ this ->tryDeserializeError ($ response , BadRequest400Exception::class);
0 commit comments