@@ -679,8 +679,15 @@ private function tryDeserializeError(array $response, string $errorClass): Elast
679679 {
680680 $ error = $ this ->serializer ->deserialize ($ response ['body ' ], $ response ['transfer_stats ' ]);
681681 if (is_array ($ error ) === true ) {
682+ if (isset ($ error ['error ' ]) === false ) {
683+ // <2.0 "i just blew up" nonstructured exception
684+ // $error is an array but we don't know the format, reuse the response body instead
685+ // added json_encode to convert into a string
686+ return new $ errorClass (json_encode ($ response ['body ' ]), (int ) $ response ['status ' ]);
687+ }
688+
682689 // 2.0 structured exceptions
683- if (isset ($ error ['error ' ][ 'reason ' ]) === true ) {
690+ if (is_array ($ error ['error ' ]) && array_key_exists ( 'reason ' , $ error [ ' error ' ]) === true ) {
684691 // Try to use root cause first (only grabs the first root cause)
685692 $ root = $ error ['error ' ]['root_cause ' ];
686693 if (isset ($ root ) && isset ($ root [0 ])) {
@@ -694,18 +701,16 @@ private function tryDeserializeError(array $response, string $errorClass): Elast
694701 $ original = new $ errorClass (json_encode ($ response ['body ' ]), $ response ['status ' ]);
695702
696703 return new $ errorClass ("$ type: $ cause " , (int ) $ response ['status ' ], $ original );
697- } elseif (isset ($ error ['error ' ]) === true ) {
698- // <2.0 semi-structured exceptions
699- // added json_encode to convert into a string
700- $ original = new $ errorClass (json_encode ($ response ['body ' ]), $ response ['status ' ]);
701-
702- return new $ errorClass ($ error ['error ' ], (int ) $ response ['status ' ], $ original );
703704 }
704-
705- // <2.0 "i just blew up" nonstructured exception
706- // $error is an array but we don't know the format, reuse the response body instead
705+ // <2.0 semi-structured exceptions
707706 // added json_encode to convert into a string
708- return new $ errorClass (json_encode ($ response ['body ' ]), (int ) $ response ['status ' ]);
707+ $ original = new $ errorClass (json_encode ($ response ['body ' ]), $ response ['status ' ]);
708+
709+ $ errorEncoded = $ error ['error ' ];
710+ if (is_array ($ errorEncoded )) {
711+ $ errorEncoded = json_encode ($ errorEncoded );
712+ }
713+ return new $ errorClass ($ errorEncoded , (int ) $ response ['status ' ], $ original );
709714 }
710715
711716 // if responseBody is not string, we convert it so it can be used as Exception message
0 commit comments