From bbd25000b113a0b87b28a6dc42eb0dce5e189fa3 Mon Sep 17 00:00:00 2001 From: Laura Trotta <153528055+l-trotta@users.noreply.github.com> Date: Fri, 4 Sep 2026 16:31:09 +0200 Subject: [PATCH] proper message for nested errors (#1335) --- .../_types/ElasticsearchException.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/ElasticsearchException.java b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/ElasticsearchException.java index bad5ca1067..74b927db6c 100644 --- a/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/ElasticsearchException.java +++ b/java-client/src/main/java/co/elastic/clients/elasticsearch/_types/ElasticsearchException.java @@ -22,6 +22,7 @@ import co.elastic.clients.transport.http.TransportHttpClient; import javax.annotation.Nullable; +import java.util.Objects; /** * Exception thrown by API client methods when Elasticsearch could not accept or @@ -40,12 +41,37 @@ public class ElasticsearchException extends RuntimeException { public ElasticsearchException(String endpointId, ErrorResponse response, @Nullable TransportHttpClient.Response httpResponse) { - super("[" + endpointId + "] failed: [" + response.error().type() + "] " + response.error().reason()); + super(buildCompleteErrorMessage(endpointId, response)); this.response = response; this.endpointId = endpointId; this.httpResponse = httpResponse; } + private static String buildCompleteErrorMessage(String endpointId, ErrorResponse response) { + ErrorCause error = response.error(); + StringBuilder message = new StringBuilder(); + message.append("[").append(endpointId).append("] failed: [").append(error.type()).append("] ") + .append(error.reason()); + + // Nested errors + for (ErrorCause cause = error.causedBy(); cause != null; cause = cause.causedBy()) { + message.append("; caused by: [").append(cause.type()).append("] ").append(cause.reason()); + } + + // Root causes carry the actual error for generic errors such "all shards failed" + for (ErrorCause rootCause : error.rootCause()) { + // Avoid repeating same message + if (Objects.equals(rootCause.type(), error.type()) && Objects.equals(rootCause.reason(), error.reason())) { + continue; + } + message.append("; root cause: [").append(rootCause.type()).append("] ").append(rootCause.reason()); + for (ErrorCause cause = rootCause.causedBy(); cause != null; cause = cause.causedBy()) { + message.append("; caused by: [").append(cause.type()).append("] ").append(cause.reason()); + } + } + return message.toString(); + } + public ElasticsearchException(String endpointId, ErrorResponse response) { this(endpointId, response, null); }