Skip to content

Commit 517abe4

Browse files
ConnectTransportException returns retryable BAD_GATEWAY (#118681)
ConnectTransportException and its subclasses previous translated to a INTERNAL_SERVER_ERROR HTTP 500 code. We are changing it to 502 BAD_GATEWAY so that users may choose to retry it on connectivity issues. Related ES-10214 Closes #118320
1 parent 4b90f01 commit 517abe4

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

docs/changelog/118681.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 118681
2+
summary: '`ConnectTransportException` returns retryable BAD_GATEWAY'
3+
area: Network
4+
type: enhancement
5+
issues:
6+
- 118320

server/src/main/java/org/elasticsearch/transport/ConnectTransportException.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.cluster.node.DiscoveryNode;
1414
import org.elasticsearch.common.io.stream.StreamInput;
1515
import org.elasticsearch.common.io.stream.StreamOutput;
16+
import org.elasticsearch.rest.RestStatus;
1617

1718
import java.io.IOException;
1819

@@ -41,6 +42,18 @@ public ConnectTransportException(StreamInput in) throws IOException {
4142
}
4243
}
4344

45+
/**
46+
* The ES REST API is a gateway to a single or multiple clusters. If there is an error connecting to other servers, then we should
47+
* return a 502 BAD_GATEWAY status code instead of the parent class' 500 INTERNAL_SERVER_ERROR. Clients tend to retry on a 502 but not
48+
* on a 500, and retrying may help on a connection error.
49+
*
50+
* @return a {@link RestStatus#BAD_GATEWAY} code
51+
*/
52+
@Override
53+
public final RestStatus status() {
54+
return RestStatus.BAD_GATEWAY;
55+
}
56+
4457
@Override
4558
protected void writeTo(StreamOutput out, Writer<Throwable> nestedExceptionsWriter) throws IOException {
4659
super.writeTo(out, nestedExceptionsWriter);

server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ public void testConnectTransportException() throws IOException {
409409
ex = serialize(new ConnectTransportException(node, "msg", "action", new NullPointerException()));
410410
assertEquals("[][" + transportAddress + "][action] msg", ex.getMessage());
411411
assertThat(ex.getCause(), instanceOf(NullPointerException.class));
412+
assertEquals(RestStatus.BAD_GATEWAY, ex.status());
412413
}
413414

414415
public void testSearchPhaseExecutionException() throws IOException {

0 commit comments

Comments
 (0)