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."));
}
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());
}
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();
}
}
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());
}
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);
}
}
Aggregations