Search in sources :

Example 1 with MissingRequiredPropertyException

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());
}
Also used : Buckets(org.opensearch.client.opensearch._types.aggregations.Buckets) Arrays(java.util.Arrays) Aggregation(org.opensearch.client.opensearch._types.aggregations.Aggregation) GetRequest(org.opensearch.client.opensearch.core.GetRequest) HashMap(java.util.HashMap) Aggregate(org.opensearch.client.opensearch._types.aggregations.Aggregate) RangeBucket(org.opensearch.client.opensearch._types.aggregations.RangeBucket) ValueCountAggregation(org.opensearch.client.opensearch._types.aggregations.ValueCountAggregation) ObjectBuilder(org.opensearch.client.util.ObjectBuilder) TotalHitsRelation(org.opensearch.client.opensearch.core.search.TotalHitsRelation) ApiTypeHelper(org.opensearch.client.util.ApiTypeHelper) Map(java.util.Map) HitsMetadata(org.opensearch.client.opensearch.core.search.HitsMetadata) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) ErrorCause(org.opensearch.client.opensearch._types.ErrorCause) JsonData(org.opensearch.client.json.JsonData) Test(org.junit.Test) TotalHits(org.opensearch.client.opensearch.core.search.TotalHits) Collectors(java.util.stream.Collectors) IntervalsQuery(org.opensearch.client.opensearch._types.query_dsl.IntervalsQuery) DateRangeAggregate(org.opensearch.client.opensearch._types.aggregations.DateRangeAggregate) MissingRequiredPropertyException(org.opensearch.client.util.MissingRequiredPropertyException) List(java.util.List) SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) CardinalityAggregate(org.opensearch.client.opensearch._types.aggregations.CardinalityAggregate) FieldAndFormat(org.opensearch.client.opensearch._types.query_dsl.FieldAndFormat) Collections(java.util.Collections) ApiTypeHelper(org.opensearch.client.util.ApiTypeHelper) GetRequest(org.opensearch.client.opensearch.core.GetRequest) MissingRequiredPropertyException(org.opensearch.client.util.MissingRequiredPropertyException) Test(org.junit.Test)

Example 2 with MissingRequiredPropertyException

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());
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) ResponseException(org.opensearch.client.ResponseException) InputStream(java.io.InputStream) TransportException(org.opensearch.client.transport.TransportException) BooleanEndpoint(org.opensearch.client.transport.endpoints.BooleanEndpoint) Endpoint(org.opensearch.client.transport.Endpoint) JsonEndpoint(org.opensearch.client.transport.JsonEndpoint) TransportException(org.opensearch.client.transport.TransportException) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) IOException(java.io.IOException) MissingRequiredPropertyException(org.opensearch.client.util.MissingRequiredPropertyException) ResponseException(org.opensearch.client.ResponseException) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) MissingRequiredPropertyException(org.opensearch.client.util.MissingRequiredPropertyException) JsonParser(jakarta.json.stream.JsonParser)

Aggregations

MissingRequiredPropertyException (org.opensearch.client.util.MissingRequiredPropertyException)2 JsonParser (jakarta.json.stream.JsonParser)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 HttpEntity (org.apache.http.HttpEntity)1 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)1 Test (org.junit.Test)1 ResponseException (org.opensearch.client.ResponseException)1 JsonData (org.opensearch.client.json.JsonData)1 ErrorCause (org.opensearch.client.opensearch._types.ErrorCause)1 OpenSearchException (org.opensearch.client.opensearch._types.OpenSearchException)1 Aggregate (org.opensearch.client.opensearch._types.aggregations.Aggregate)1