Skip to content

Commit

Permalink
add ResponseException#status
Browse files Browse the repository at this point in the history
this allows accessing the HTTP status code of the response when an API
returns a `ResponseException`. this was not possible through
`getResponse` since `Response` itself is package-private and thus its
members (even though they are marked as `public`) are not accessible to
external consumers.

as there's no test coverage for `ResponseException` i haven't added any
for the new API either, as it is a trivial API and it was unclear how
adding test coverage for it would fit into the existing test coverage.

solves #749

Signed-off-by: Ralph Ursprung <Ralph.Ursprung@avaloq.com>
  • Loading branch information
rursprung committed Dec 4, 2023
1 parent 9213138 commit 2ca1627
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This section is for maintaining a changelog for all breaking changes for the cli

### Added
- Document HTTP/2 support ([#330](https://github.com/opensearch-project/opensearch-java/pull/330))
- Expose HTTP status code through `ResponseException#status` ([#756](https://github.com/opensearch-project/opensearch-java/pull/756))

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static String buildMessage(Response response) throws IOException {
response.getRequestLine().getMethod(),
response.getHost(),
response.getRequestLine().getUri(),
response.getStatusLine().toString()
response.getStatusLine()
);

if (response.hasWarnings()) {
Expand All @@ -73,7 +73,7 @@ static String buildMessage(Response response) throws IOException {

HttpEntity entity = response.getEntity();
if (entity != null) {
if (entity.isRepeatable() == false) {
if (!entity.isRepeatable()) {
entity = new BufferedHttpEntity(entity);
response.getHttpResponse().setEntity(entity);
}
Expand All @@ -92,4 +92,12 @@ static String buildMessage(Response response) throws IOException {
public Response getResponse() {
return response;
}

/**
* Status code returned by Elasticsearch. Shortcut for
* {@code response().status()}.
*/
public int status() {
return this.response.getStatusLine().getStatusCode();
}
}

0 comments on commit 2ca1627

Please sign in to comment.