3636import co .elastic .clients .util .ApiTypeHelper ;
3737import co .elastic .clients .util .BinaryData ;
3838import co .elastic .clients .util .MissingRequiredPropertyException ;
39+ import jakarta .json .JsonException ;
3940import jakarta .json .stream .JsonGenerator ;
4041import jakarta .json .stream .JsonParser ;
4142import org .apache .http .HttpEntity ;
@@ -85,7 +86,7 @@ public class RestClientTransport implements ElasticsearchTransport {
8586
8687 /**
8788 * The {@code Future} implementation returned by async requests.
88- * It wraps the RestClient's cancellable and progagates cancellation.
89+ * It wraps the RestClient's cancellable and propagates cancellation.
8990 */
9091 private static class RequestFuture <T > extends CompletableFuture <T > {
9192 private volatile Cancellable cancellable ;
@@ -310,6 +311,7 @@ private <ResponseT, ErrorT> ResponseT getHighLevelResponse(
310311 JsonpDeserializer <ErrorT > errorDeserializer = endpoint .errorDeserializer (statusCode );
311312 if (errorDeserializer == null ) {
312313 throw new TransportException (
314+ statusCode ,
313315 "Request failed with status code '" + statusCode + "'" ,
314316 endpoint .id (), new ResponseException (clientResp )
315317 );
@@ -318,6 +320,7 @@ private <ResponseT, ErrorT> ResponseT getHighLevelResponse(
318320 HttpEntity entity = clientResp .getEntity ();
319321 if (entity == null ) {
320322 throw new TransportException (
323+ statusCode ,
321324 "Expecting a response body, but none was sent" ,
322325 endpoint .id (), new ResponseException (clientResp )
323326 );
@@ -333,14 +336,17 @@ private <ResponseT, ErrorT> ResponseT getHighLevelResponse(
333336 // TODO: have the endpoint provide the exception constructor
334337 throw new ElasticsearchException (endpoint .id (), (ErrorResponse ) error );
335338 }
336- } catch (MissingRequiredPropertyException errorEx ) {
339+ } catch (JsonException | MissingRequiredPropertyException errorEx ) {
337340 // Could not decode exception, try the response type
338341 try {
339342 ResponseT response = decodeResponse (statusCode , entity , clientResp , endpoint );
340343 return response ;
341344 } catch (Exception respEx ) {
342345 // No better luck: throw the original error decoding exception
343- throw new TransportException ("Failed to decode error response" , endpoint .id (), new ResponseException (clientResp ));
346+ throw new TransportException (statusCode ,
347+ "Failed to decode error response, check exception cause for additional details" , endpoint .id (),
348+ new ResponseException (clientResp )
349+ );
344350 }
345351 }
346352 } else {
@@ -368,6 +374,7 @@ private <ResponseT> ResponseT decodeResponse(
368374 // Expecting a body
369375 if (entity == null ) {
370376 throw new TransportException (
377+ statusCode ,
371378 "Expecting a response body, but none was sent" ,
372379 endpoint .id (), new ResponseException (clientResp )
373380 );
@@ -395,7 +402,7 @@ private <ResponseT> ResponseT decodeResponse(
395402 return response ;
396403
397404 } else {
398- throw new TransportException ("Unhandled endpoint type: '" + endpoint .getClass ().getName () + "'" , endpoint .id ());
405+ throw new TransportException (statusCode , "Unhandled endpoint type: '" + endpoint .getClass ().getName () + "'" , endpoint .id ());
399406 }
400407 }
401408
@@ -411,6 +418,7 @@ private void checkProductHeader(Response clientResp, Endpoint<?, ?, ?> endpoint)
411418 return ;
412419 }
413420 throw new TransportException (
421+ clientResp .getStatusLine ().getStatusCode (),
414422 "Missing [X-Elastic-Product] header. Please check that you are connecting to an Elasticsearch "
415423 + "instance, and that any networking filters are preserving that header." ,
416424 endpoint .id (),
@@ -419,7 +427,9 @@ private void checkProductHeader(Response clientResp, Endpoint<?, ?, ?> endpoint)
419427 }
420428
421429 if (!"Elasticsearch" .equals (header )) {
422- throw new TransportException ("Invalid value '" + header + "' for 'X-Elastic-Product' header." ,
430+ throw new TransportException (
431+ clientResp .getStatusLine ().getStatusCode (),
432+ "Invalid value '" + header + "' for 'X-Elastic-Product' header." ,
423433 endpoint .id (),
424434 new ResponseException (clientResp )
425435 );
0 commit comments