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