Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ResponseException#status #756

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
rursprung marked this conversation as resolved.
Show resolved Hide resolved
);

if (response.hasWarnings()) {
Expand Down Expand Up @@ -92,4 +92,11 @@ static String buildMessage(Response response) throws IOException {
public Response getResponse() {
return response;
}

/**
* HTTP status code returned by OpenSearch.
*/
public int status() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Should it be status or getStatus? What's the convention?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i took this 1:1 from OpenSearchException, presuming that this was the accepted convention. happy to change it to getStatus

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've left it at status for now pending your feedback

return this.response.getStatusLine().getStatusCode();
rursprung marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.opensearch.client.transport.httpclient5;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.io.IOException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.HttpVersion;
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
import org.apache.hc.core5.http.message.RequestLine;
import org.junit.Test;

public class ResponseExceptionTest {

@Test
public void testStatus() throws IOException {
final var response = this.buildResponseException(HttpStatus.SC_BAD_REQUEST);
assertThat(response.status(), equalTo(HttpStatus.SC_BAD_REQUEST));
}

private ResponseException buildResponseException(final int statusCode) throws IOException {
return new ResponseException(this.buildTestResponse(statusCode));
}

private Response buildTestResponse(final int statusCode) {
return new Response(
new RequestLine("GET", "/", HttpVersion.HTTP_1_1),
new HttpHost("localhost"),
new BasicClassicHttpResponse(statusCode)
);
}

}
Loading