use of org.opensearch.client.util.MissingRequiredPropertyException in project opensearch-java by opensearch-project.
the class ClassStructureTest method testRequiredProperty.
@Test
public void testRequiredProperty() {
// All required properties present
GetRequest r = GetRequest.of(b -> b.index("foo").id("bar"));
// Missing id property throws an exception
MissingRequiredPropertyException ex = assertThrows(MissingRequiredPropertyException.class, () -> {
GetRequest r1 = GetRequest.of(b -> b.index("foo"));
});
assertEquals("id", ex.getPropertyName());
// Disable checks, missing id property is accepted.
try (ApiTypeHelper.DisabledChecksHandle h = ApiTypeHelper.DANGEROUS_disableRequiredPropertiesCheck(true)) {
GetRequest r1 = GetRequest.of(b -> b.index("foo"));
assertNull(r1.id());
}
// Checks are enabled again after the try block
ex = assertThrows(MissingRequiredPropertyException.class, () -> {
GetRequest r1 = GetRequest.of(b -> b.index("foo"));
});
assertEquals("id", ex.getPropertyName());
}
use of org.opensearch.client.util.MissingRequiredPropertyException in project opensearch-java by opensearch-project.
the class RestClientTransport method getHighLevelResponse.
private <ResponseT, ErrorT> ResponseT getHighLevelResponse(org.opensearch.client.Response clientResp, Endpoint<?, ResponseT, ErrorT> endpoint) throws IOException {
try {
int statusCode = clientResp.getStatusLine().getStatusCode();
if (endpoint.isError(statusCode)) {
JsonpDeserializer<ErrorT> errorDeserializer = endpoint.errorDeserializer(statusCode);
if (errorDeserializer == null) {
throw new TransportException("Request failed with status code '" + statusCode + "'", new ResponseException(clientResp));
}
HttpEntity entity = clientResp.getEntity();
if (entity == null) {
throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp));
}
// We may have to replay it.
entity = new BufferedHttpEntity(entity);
try {
InputStream content = entity.getContent();
try (JsonParser parser = mapper.jsonProvider().createParser(content)) {
ErrorT error = errorDeserializer.deserialize(parser, mapper);
// TODO: have the endpoint provide the exception constructor
throw new OpenSearchException((ErrorResponse) error);
}
} catch (MissingRequiredPropertyException errorEx) {
// Could not decode exception, try the response type
try {
ResponseT response = decodeResponse(statusCode, entity, clientResp, endpoint);
return response;
} catch (Exception respEx) {
// No better luck: throw the original error decoding exception
throw new TransportException("Failed to decode error response", new ResponseException(clientResp));
}
}
} else {
return decodeResponse(statusCode, clientResp.getEntity(), clientResp, endpoint);
}
} finally {
EntityUtils.consume(clientResp.getEntity());
}
}
Aggregations