use of org.opensearch.client.ResponseException in project opensearch-java by opensearch-project.
the class RequestOptionsTest method getProps.
private Properties getProps(OpenSearchClient client) throws IOException {
ResponseException ex = assertThrows(ResponseException.class, client::info);
assertEquals(418, ex.getResponse().getStatusLine().getStatusCode());
Properties result = new Properties();
result.load(ex.getResponse().getEntity().getContent());
return result;
}
use of org.opensearch.client.ResponseException in project opensearch-java by opensearch-project.
the class RestClientTransport method decodeResponse.
private <ResponseT> ResponseT decodeResponse(int statusCode, @Nullable HttpEntity entity, Response clientResp, Endpoint<?, ResponseT, ?> endpoint) throws IOException {
if (endpoint instanceof BooleanEndpoint) {
BooleanEndpoint<?> bep = (BooleanEndpoint<?>) endpoint;
@SuppressWarnings("unchecked") ResponseT response = (ResponseT) new BooleanResponse(bep.getResult(statusCode));
return response;
} else if (endpoint instanceof JsonEndpoint) {
@SuppressWarnings("unchecked") JsonEndpoint<?, ResponseT, ?> jsonEndpoint = (JsonEndpoint<?, ResponseT, ?>) endpoint;
// Successful response
ResponseT response = null;
JsonpDeserializer<ResponseT> responseParser = jsonEndpoint.responseDeserializer();
if (responseParser != null) {
// Expecting a body
if (entity == null) {
throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp));
}
InputStream content = entity.getContent();
try (JsonParser parser = mapper.jsonProvider().createParser(content)) {
response = responseParser.deserialize(parser, mapper);
}
;
}
return response;
} else {
throw new TransportException("Unhandled endpoint type: '" + endpoint.getClass().getName() + "'");
}
}
use of org.opensearch.client.ResponseException in project OpenSearch by opensearch-project.
the class OpenSearchNodesSnifferTests method testSniffNodes.
public void testSniffNodes() throws IOException {
HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
try (RestClient restClient = RestClient.builder(httpHost).build()) {
OpenSearchNodesSniffer sniffer = new OpenSearchNodesSniffer(restClient, sniffRequestTimeout, scheme);
try {
List<Node> sniffedNodes = sniffer.sniff();
if (sniffResponse.isFailure) {
fail("sniffNodes should have failed");
}
assertEquals(sniffResponse.result, sniffedNodes);
} catch (ResponseException e) {
Response response = e.getResponse();
if (sniffResponse.isFailure) {
final String errorPrefix = "method [GET], host [" + httpHost + "], URI [/_nodes/http?timeout=" + sniffRequestTimeout + "ms], status line [HTTP/1.1";
assertThat(e.getMessage(), startsWith(errorPrefix));
assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));
assertThat(response.getHost(), equalTo(httpHost));
assertThat(response.getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode));
assertThat(response.getRequestLine().toString(), equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1"));
} else {
fail("sniffNodes should have succeeded: " + response.getStatusLine());
}
}
}
}
use of org.opensearch.client.ResponseException in project OpenSearch by opensearch-project.
the class RequestsWithoutContentIT method testPutScriptMissingBody.
public void testPutScriptMissingBody() throws IOException {
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/_scripts/lang")));
assertResponseException(responseException, "request body is required");
}
use of org.opensearch.client.ResponseException in project OpenSearch by opensearch-project.
the class RequestsWithoutContentIT method testPutMappingsMissingBody.
public void testPutMappingsMissingBody() throws IOException {
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/test_index/_mapping")));
assertResponseException(responseException, "request body is required");
}
Aggregations