Search in sources :

Example 6 with InvalidArgumentException

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

the class ClusterErrorIntegrationTest method failsOnInvalidConnectionString.

@Test
void failsOnInvalidConnectionString() {
    InvalidArgumentException exception = assertThrows(InvalidArgumentException.class, () -> Cluster.connect("localhost:8091", "foo", "bar"));
    assertTrue(exception.getMessage().contains("Please omit the port and use \"localhost\" instead."));
    exception = assertThrows(InvalidArgumentException.class, () -> Cluster.connect("1.2.3.4:8091", "foo", "bar"));
    assertTrue(exception.getMessage().contains("Please omit the port and use \"1.2.3.4\" instead."));
}
Also used : InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Test(org.junit.jupiter.api.Test)

Example 7 with InvalidArgumentException

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

the class BackoffTest method exponentialRejectsMaxLowerThanFirst.

@Test
void exponentialRejectsMaxLowerThanFirst() {
    InvalidArgumentException ex = assertThrows(InvalidArgumentException.class, () -> Backoff.exponential(Duration.ofSeconds(2), Duration.ofSeconds(1), 1, false));
    assertEquals("maxBackoff must be >= firstBackoff", ex.getMessage());
    ex = assertThrows(InvalidArgumentException.class, () -> Backoff.exponential(Duration.ofSeconds(2), Duration.ofSeconds(1), 1, true));
    assertEquals("maxBackoff must be >= firstBackoff", ex.getMessage());
}
Also used : InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Test(org.junit.jupiter.api.Test)

Example 8 with InvalidArgumentException

use of com.couchbase.client.core.error.InvalidArgumentException in project spring-data-couchbase by spring-projects.

the class QueryCriteria method maybeWrapValue.

/**
 * Possibly convert an operand to a positional or named parameter
 *
 * @param paramIndexPtr - this is a reference to the parameter index to be used for positional parameters There may
 *          already be positional parameters in the beginning of the statement, so it may not always start at 1. If it
 *          has the value -1, the query is using named parameters. If the pointer is null, the query is not using
 *          parameters.
 * @param parameters - parameters of the query. If operands are parameterized, their values are added to parameters
 * @return string containing part of N1QL query
 */
private String maybeWrapValue(N1QLExpression key, Object value, int[] paramIndexPtr, JsonValue parameters, CouchbaseConverter converter) {
    if (paramIndexPtr != null) {
        if (paramIndexPtr[0] >= 0) {
            JsonArray params = (JsonArray) parameters;
            if (value instanceof Object[] || value instanceof Collection) {
                addAsCollection(params, asCollection(value), converter);
            } else {
                params.add(convert(converter, value));
            }
            // these are generated in order
            return "$" + (++paramIndexPtr[0]);
        } else {
            JsonObject params = (JsonObject) parameters;
            // from StringBasedN1qlQueryParser.getNamedPlaceholderValues()
            try {
                params.put(key.toString(), convert(converter, value));
            } catch (InvalidArgumentException iae) {
                if (value instanceof Object[]) {
                    params.put(key.toString(), JsonArray.from((Object[]) value));
                } else {
                    throw iae;
                }
            }
            return "$" + key;
        }
    }
    if (value instanceof String) {
        return "\"" + value + "\"";
    } else if (value == null) {
        return "null";
    } else if (value instanceof Object[]) {
        // convert array into sequence of comma-separated values
        StringBuilder l = new StringBuilder();
        l.append("[");
        Object[] array = (Object[]) value;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                l.append(",");
            }
            l.append(maybeWrapValue(null, array[i], null, null, converter));
        }
        l.append("]");
        return l.toString();
    } else {
        return value.toString();
    }
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Collection(java.util.Collection) JsonObject(com.couchbase.client.java.json.JsonObject) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 9 with InvalidArgumentException

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

the class CoreAnalyticsLinkManager method translateCompilationFailureToInvalidArgument.

private static RuntimeException translateCompilationFailureToInvalidArgument(Throwable t) {
    if (!(t.getCause() instanceof CompilationFailureException)) {
        CbThrowables.throwIfUnchecked(t);
        throw new CouchbaseException(t.getMessage(), t);
    }
    CompilationFailureException e = (CompilationFailureException) t.getCause();
    String message = ((AnalyticsErrorContext) e.context()).errors().get(0).message();
    throw new InvalidArgumentException(message, e.getCause(), e.context());
}
Also used : AnalyticsErrorContext(com.couchbase.client.core.error.context.AnalyticsErrorContext) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Builder.newQueryString(com.couchbase.client.core.endpoint.http.CoreHttpRequest.Builder.newQueryString) CompilationFailureException(com.couchbase.client.core.error.CompilationFailureException)

Example 10 with InvalidArgumentException

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

the class BuilderPropertySetter method set.

/**
 * @throws InvalidPropertyException if the property could not be applied to the builder
 */
public void set(Object builder, String propertyName, String propertyValue) {
    // By convention, builder methods that return child builders have names ending with this.
    final String CHILD_BUILDER_ACCESSOR_SUFFIX = "Config";
    try {
        final List<String> propertyComponents = Arrays.asList(propertyName.split("\\.", -1));
        final List<String> pathToBuilder = propertyComponents.subList(0, propertyComponents.size() - 1);
        final String setterName = propertyComponents.get(propertyComponents.size() - 1);
        for (String pathComponent : pathToBuilder) {
            try {
                final String childBuilderAccessor = pathComponent + CHILD_BUILDER_ACCESSOR_SUFFIX;
                builder = builder.getClass().getMethod(childBuilderAccessor).invoke(builder);
            } catch (NoSuchMethodException e) {
                throw InvalidArgumentException.fromMessage("Method not found: " + e.getMessage(), e);
            }
        }
        final List<Method> candidates = Arrays.stream(builder.getClass().getMethods()).filter(m -> m.getName().equals(setterName)).filter(m -> m.getParameterCount() == 1).collect(Collectors.toList());
        if (candidates.isEmpty()) {
            throw InvalidArgumentException.fromMessage("No one-arg setter for property \"" + propertyName + "\" in " + builder.getClass());
        }
        int remainingCandidates = candidates.size();
        final List<Throwable> failedCandidates = new ArrayList<>();
        for (Method setter : candidates) {
            try {
                final Object convertedValue = typeRegistry.convert(propertyValue, setter.getGenericParameterTypes()[0]);
                setter.invoke(builder, convertedValue);
            } catch (Throwable t) {
                if (candidates.size() == 1) {
                    throw t;
                }
                failedCandidates.add(t);
                if (--remainingCandidates == 0) {
                    final InvalidArgumentException e = InvalidArgumentException.fromMessage("Found multiple one-arg setters for property \"" + propertyName + "\" in " + builder.getClass() + " but none accepted the value \"" + propertyValue + "\".");
                    failedCandidates.forEach(e::addSuppressed);
                    throw e;
                }
            }
        }
    } catch (InvocationTargetException e) {
        throw InvalidPropertyException.forProperty(propertyName, propertyValue, e.getCause());
    } catch (Exception e) {
        throw InvalidPropertyException.forProperty(propertyName, propertyValue, e);
    }
}
Also used : Arrays(java.util.Arrays) Array(java.lang.reflect.Array) TypeReference(com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Method(java.lang.reflect.Method) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) Golang(com.couchbase.client.core.util.Golang) Mapper(com.couchbase.client.core.json.Mapper) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Paths(java.nio.file.Paths) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException)

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