Search in sources :

Example 1 with InvalidArgumentException

use of com.couchbase.client.core.error.InvalidArgumentException in project couchbase-jvm-clients by couchbase.

the class BuilderPropertySetterTest method setBoolean.

@Test
void setBoolean() {
    CoreEnvironment.Builder<?> builder = newEnvironmentBuilder();
    setter.set(builder, "io.queryCircuitBreaker.enabled", "true");
    setter.set(builder, "io.analyticsCircuitBreaker.enabled", "1");
    setter.set(builder, "io.viewCircuitBreaker.enabled", "false");
    setter.set(builder, "io.kvCircuitBreaker.enabled", "0");
    IoConfig io = builder.ioConfig().build();
    assertTrue(io.queryCircuitBreakerConfig().enabled());
    assertTrue(io.analyticsCircuitBreakerConfig().enabled());
    assertFalse(io.viewCircuitBreakerConfig().enabled());
    assertFalse(io.kvCircuitBreakerConfig().enabled());
    InvalidArgumentException e = assertThrows(InvalidArgumentException.class, () -> setter.set(builder, "io.kvCircuitBreaker.enabled", "TRUE"));
    assertEquals("Expected a boolean (\"true\", \"false\", \"1\", or \"0\") but got \"TRUE\".", e.getCause().getMessage());
}
Also used : InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Test(org.junit.jupiter.api.Test)

Example 2 with InvalidArgumentException

use of com.couchbase.client.core.error.InvalidArgumentException in project couchbase-jvm-clients by couchbase.

the class BuilderPropertySetterTest method setInt.

@Test
void setInt() {
    CoreEnvironment.Builder<?> builder = newEnvironmentBuilder();
    setter.set(builder, "io.maxHttpConnections", "76");
    IoConfig io = builder.ioConfig().build();
    assertEquals(76, io.maxHttpConnections());
    InvalidArgumentException e = assertThrows(InvalidArgumentException.class, () -> setter.set(builder, "io.maxHttpConnections", "garbage"));
    assertEquals("Expected an int but got \"garbage\".", e.getCause().getMessage());
}
Also used : InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Test(org.junit.jupiter.api.Test)

Example 3 with InvalidArgumentException

use of com.couchbase.client.core.error.InvalidArgumentException in project couchbase-jvm-clients by couchbase.

the class AsyncCollection method subdocGetRequest.

/**
 * Helper method to create a get request for a subdoc fetch.
 *
 * @param id the document id which is used to uniquely identify it.
 * @param opts custom options to change the default behavior.
 * @return the subdoc get request.
 */
@Stability.Internal
SubdocGetRequest subdocGetRequest(final String id, final GetOptions.Built opts) {
    try {
        notNullOrEmpty(id, "Id");
        if (opts.withExpiry()) {
            if (opts.projections().size() > 15) {
                throw InvalidArgumentException.fromMessage("Only a maximum of 16 fields can be " + "projected per request due to a server limitation (includes the expiration macro as one field).");
            }
        } else {
            if (opts.projections().size() > 16) {
                throw InvalidArgumentException.fromMessage("Only a maximum of 16 fields can be " + "projected per request due to a server limitation.");
            }
        }
    } catch (Exception cause) {
        throw new InvalidArgumentException("Argument validation failed", cause, ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    }
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    List<SubdocGetRequest.Command> commands = new ArrayList<>(16);
    if (!opts.projections().isEmpty()) {
        if (opts.projections().size() > 16) {
            throw new UnsupportedOperationException("Only a maximum of 16 fields can be " + "projected per request.");
        }
        List<String> projections = opts.projections();
        for (int i = 0; i < projections.size(); i++) {
            commands.add(new SubdocGetRequest.Command(SubdocCommandType.GET, projections.get(i), false, commands.size()));
        }
    } else {
        commands.add(new SubdocGetRequest.Command(SubdocCommandType.GET_DOC, "", false, commands.size()));
    }
    if (opts.withExpiry()) {
        // xattrs must go first
        commands.add(0, new SubdocGetRequest.Command(SubdocCommandType.GET, LookupInMacro.EXPIRY_TIME, true, commands.size()));
        // JSON compat flags.
        if (opts.projections().isEmpty()) {
            commands.add(1, new SubdocGetRequest.Command(SubdocCommandType.GET, LookupInMacro.FLAGS, true, commands.size()));
        }
    }
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_LOOKUP_IN, opts.parentSpan().orElse(null));
    SubdocGetRequest request = new SubdocGetRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, (byte) 0x00, commands, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : ArrayList(java.util.ArrayList) Duration(java.time.Duration) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest)

Example 4 with InvalidArgumentException

use of com.couchbase.client.core.error.InvalidArgumentException in project couchbase-jvm-clients by couchbase.

the class JsonObjectTest method shouldThrowOnBadSubMap.

@Test
void shouldThrowOnBadSubMap() {
    Map<Integer, String> badMap1 = mapOf(1, "test");
    Map<String, Object> badMap2 = mapOf("key1", new CloneNotSupportedException());
    InvalidArgumentException e = assertThrows(InvalidArgumentException.class, () -> JsonObject.from(mapOf("subMap", badMap1)));
    assertTrue(e.getCause() instanceof ClassCastException);
    assertThrows(InvalidArgumentException.class, () -> JsonObject.from(mapOf("subMap", badMap2)));
}
Also used : BigInteger(java.math.BigInteger) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Test(org.junit.jupiter.api.Test)

Example 5 with InvalidArgumentException

use of com.couchbase.client.core.error.InvalidArgumentException in project couchbase-jvm-clients by couchbase.

the class EventingFunction method fromExportedFunctions.

/**
 * Creates a list of {@link EventingFunction}s from a raw JSON, usually exported from the server UI.
 *
 * @param encoded the encoded functions to load.
 * @return the created list of {@link EventingFunction}s.
 */
@Stability.Volatile
public static List<EventingFunction> fromExportedFunctions(final byte[] encoded) {
    JsonNode encodedFunctions = Mapper.decodeIntoTree(encoded);
    if (!encodedFunctions.isArray()) {
        throw new InvalidArgumentException("The encoded JSON must be a JSON array of functions", null, null);
    }
    List<EventingFunction> functions = new ArrayList<>();
    for (JsonNode function : encodedFunctions) {
        functions.add(fromFunction(Mapper.encodeAsBytes(function)));
    }
    return functions;
}
Also used : InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) ArrayList(java.util.ArrayList) JsonNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode)

Aggregations

InvalidArgumentException (com.couchbase.client.core.error.InvalidArgumentException)17 Test (org.junit.jupiter.api.Test)10 ArrayList (java.util.ArrayList)4 Duration (java.time.Duration)3 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)2 JsonNode (com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode)2 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)2 BigInteger (java.math.BigInteger)2 Collection (java.util.Collection)2 TypeReference (com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference)1 Builder.newQueryString (com.couchbase.client.core.endpoint.http.CoreHttpRequest.Builder.newQueryString)1 CompilationFailureException (com.couchbase.client.core.error.CompilationFailureException)1 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 AnalyticsErrorContext (com.couchbase.client.core.error.context.AnalyticsErrorContext)1 Mapper (com.couchbase.client.core.json.Mapper)1 SubdocGetRequest (com.couchbase.client.core.msg.kv.SubdocGetRequest)1 UnlockRequest (com.couchbase.client.core.msg.kv.UnlockRequest)1 ServiceType (com.couchbase.client.core.service.ServiceType)1 Golang (com.couchbase.client.core.util.Golang)1 JsonArray (com.couchbase.client.java.json.JsonArray)1