36
36
import co .elastic .clients .util .ApiTypeHelper ;
37
37
import co .elastic .clients .util .BinaryData ;
38
38
import co .elastic .clients .util .MissingRequiredPropertyException ;
39
+ import jakarta .json .JsonException ;
39
40
import jakarta .json .stream .JsonGenerator ;
40
41
import jakarta .json .stream .JsonParser ;
41
42
import org .apache .http .HttpEntity ;
@@ -85,7 +86,7 @@ public class RestClientTransport implements ElasticsearchTransport {
85
86
86
87
/**
87
88
* 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.
89
90
*/
90
91
private static class RequestFuture <T > extends CompletableFuture <T > {
91
92
private volatile Cancellable cancellable ;
@@ -310,6 +311,7 @@ private <ResponseT, ErrorT> ResponseT getHighLevelResponse(
310
311
JsonpDeserializer <ErrorT > errorDeserializer = endpoint .errorDeserializer (statusCode );
311
312
if (errorDeserializer == null ) {
312
313
throw new TransportException (
314
+ statusCode ,
313
315
"Request failed with status code '" + statusCode + "'" ,
314
316
endpoint .id (), new ResponseException (clientResp )
315
317
);
@@ -318,6 +320,7 @@ private <ResponseT, ErrorT> ResponseT getHighLevelResponse(
318
320
HttpEntity entity = clientResp .getEntity ();
319
321
if (entity == null ) {
320
322
throw new TransportException (
323
+ statusCode ,
321
324
"Expecting a response body, but none was sent" ,
322
325
endpoint .id (), new ResponseException (clientResp )
323
326
);
@@ -333,14 +336,17 @@ private <ResponseT, ErrorT> ResponseT getHighLevelResponse(
333
336
// TODO: have the endpoint provide the exception constructor
334
337
throw new ElasticsearchException (endpoint .id (), (ErrorResponse ) error );
335
338
}
336
- } catch (MissingRequiredPropertyException errorEx ) {
339
+ } catch (JsonException | MissingRequiredPropertyException errorEx ) {
337
340
// Could not decode exception, try the response type
338
341
try {
339
342
ResponseT response = decodeResponse (statusCode , entity , clientResp , endpoint );
340
343
return response ;
341
344
} catch (Exception respEx ) {
342
345
// 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
+ );
344
350
}
345
351
}
346
352
} else {
@@ -368,6 +374,7 @@ private <ResponseT> ResponseT decodeResponse(
368
374
// Expecting a body
369
375
if (entity == null ) {
370
376
throw new TransportException (
377
+ statusCode ,
371
378
"Expecting a response body, but none was sent" ,
372
379
endpoint .id (), new ResponseException (clientResp )
373
380
);
@@ -395,7 +402,7 @@ private <ResponseT> ResponseT decodeResponse(
395
402
return response ;
396
403
397
404
} 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 ());
399
406
}
400
407
}
401
408
@@ -411,6 +418,7 @@ private void checkProductHeader(Response clientResp, Endpoint<?, ?, ?> endpoint)
411
418
return ;
412
419
}
413
420
throw new TransportException (
421
+ clientResp .getStatusLine ().getStatusCode (),
414
422
"Missing [X-Elastic-Product] header. Please check that you are connecting to an Elasticsearch "
415
423
+ "instance, and that any networking filters are preserving that header." ,
416
424
endpoint .id (),
@@ -419,7 +427,9 @@ private void checkProductHeader(Response clientResp, Endpoint<?, ?, ?> endpoint)
419
427
}
420
428
421
429
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." ,
423
433
endpoint .id (),
424
434
new ResponseException (clientResp )
425
435
);
0 commit comments