Skip to content

Commit

Permalink
ConnectTransportException returns retryable BAD_GATEWAY (elastic#118681)
Browse files Browse the repository at this point in the history
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 elastic#118320
  • Loading branch information
DiannaHohensee authored Dec 17, 2024
1 parent 4b90f01 commit 517abe4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/118681.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 118681
summary: '`ConnectTransportException` returns retryable BAD_GATEWAY'
area: Network
type: enhancement
issues:
- 118320
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;

Expand Down Expand Up @@ -41,6 +42,18 @@ public ConnectTransportException(StreamInput in) throws IOException {
}
}

/**
* 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
* 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
* on a 500, and retrying may help on a connection error.
*
* @return a {@link RestStatus#BAD_GATEWAY} code
*/
@Override
public final RestStatus status() {
return RestStatus.BAD_GATEWAY;
}

@Override
protected void writeTo(StreamOutput out, Writer<Throwable> nestedExceptionsWriter) throws IOException {
super.writeTo(out, nestedExceptionsWriter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ public void testConnectTransportException() throws IOException {
ex = serialize(new ConnectTransportException(node, "msg", "action", new NullPointerException()));
assertEquals("[][" + transportAddress + "][action] msg", ex.getMessage());
assertThat(ex.getCause(), instanceOf(NullPointerException.class));
assertEquals(RestStatus.BAD_GATEWAY, ex.status());
}

public void testSearchPhaseExecutionException() throws IOException {
Expand Down

0 comments on commit 517abe4

Please sign in to comment.