Search in sources :

Example 1 with ErrorContext

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

the class CouchbaseExceptionTest method includesErrorContextInMessage.

@Test
void includesErrorContextInMessage() {
    String message = "my message";
    ErrorContext ctx = new ErrorContext(ResponseStatus.SUCCESS) {

        @Override
        public void injectExportableParams(Map<String, Object> input) {
            input.put("foo", "bar");
        }
    };
    CouchbaseException ex = new CouchbaseException(message, ctx);
    assertEquals("my message {\"foo\":\"bar\"}", ex.getMessage());
    assertEquals("com.couchbase.client.core.error.CouchbaseException: my message {\"foo\":\"bar\"}", ex.toString());
}
Also used : ErrorContext(com.couchbase.client.core.error.context.ErrorContext) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 2 with ErrorContext

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

the class ReplicaHelper method getAnyReplicaAsync.

/**
 * @param clientContext (nullable)
 * @param parentSpan (nullable)
 * @param responseMapper converts the GetReplicaResponse to the client's native result type
 */
public static <R> CompletableFuture<R> getAnyReplicaAsync(final Core core, final CollectionIdentifier collectionIdentifier, final String documentId, final Duration timeout, final RetryStrategy retryStrategy, final Map<String, Object> clientContext, final RequestSpan parentSpan, final Function<GetReplicaResponse, R> responseMapper) {
    RequestSpan getAnySpan = core.context().environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_GET_ANY_REPLICA, parentSpan);
    CompletableFuture<List<CompletableFuture<R>>> listOfFutures = getAllReplicasAsync(core, collectionIdentifier, documentId, timeout, retryStrategy, clientContext, getAnySpan, responseMapper);
    // Aggregating the futures here will discard the individual errors, which we don't need
    CompletableFuture<R> anyReplicaFuture = new CompletableFuture<>();
    listOfFutures.whenComplete((futures, throwable) -> {
        if (throwable != null) {
            anyReplicaFuture.completeExceptionally(throwable);
        }
        final AtomicBoolean successCompleted = new AtomicBoolean(false);
        final AtomicInteger totalCompleted = new AtomicInteger(0);
        final List<ErrorContext> nestedContexts = Collections.synchronizedList(new ArrayList<>());
        futures.forEach(individual -> individual.whenComplete((result, error) -> {
            int completed = totalCompleted.incrementAndGet();
            if (error != null) {
                if (error instanceof CompletionException && error.getCause() instanceof CouchbaseException) {
                    nestedContexts.add(((CouchbaseException) error.getCause()).context());
                }
            }
            if (result != null && successCompleted.compareAndSet(false, true)) {
                anyReplicaFuture.complete(result);
            }
            if (!successCompleted.get() && completed == futures.size()) {
                anyReplicaFuture.completeExceptionally(new DocumentUnretrievableException(new AggregateErrorContext(nestedContexts)));
            }
        }));
    });
    return anyReplicaFuture.whenComplete((getReplicaResult, throwable) -> getAnySpan.end());
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) DefaultErrorUtil.keyValueStatusToException(com.couchbase.client.core.error.DefaultErrorUtil.keyValueStatusToException) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DocumentUnretrievableException(com.couchbase.client.core.error.DocumentUnretrievableException) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) GetResponse(com.couchbase.client.core.msg.kv.GetResponse) IndividualReplicaGetFailedEvent(com.couchbase.client.core.cnc.events.request.IndividualReplicaGetFailedEvent) BucketConfig(com.couchbase.client.core.config.BucketConfig) Reactor(com.couchbase.client.core.Reactor) CommonExceptions(com.couchbase.client.core.error.CommonExceptions) Mono(reactor.core.publisher.Mono) CompletionException(java.util.concurrent.CompletionException) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Collectors(java.util.stream.Collectors) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) Flux(reactor.core.publisher.Flux) List(java.util.List) Stream(java.util.stream.Stream) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ReplicaGetRequest(com.couchbase.client.core.msg.kv.ReplicaGetRequest) ErrorContext(com.couchbase.client.core.error.context.ErrorContext) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Core(com.couchbase.client.core.Core) ReducedKeyValueErrorContext(com.couchbase.client.core.error.context.ReducedKeyValueErrorContext) Collections(java.util.Collections) AggregateErrorContext(com.couchbase.client.core.error.context.AggregateErrorContext) DocumentUnretrievableException(com.couchbase.client.core.error.DocumentUnretrievableException) ErrorContext(com.couchbase.client.core.error.context.ErrorContext) ReducedKeyValueErrorContext(com.couchbase.client.core.error.context.ReducedKeyValueErrorContext) AggregateErrorContext(com.couchbase.client.core.error.context.AggregateErrorContext) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AggregateErrorContext(com.couchbase.client.core.error.context.AggregateErrorContext) CompletionException(java.util.concurrent.CompletionException) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ErrorContext (com.couchbase.client.core.error.context.ErrorContext)2 Map (java.util.Map)2 Core (com.couchbase.client.core.Core)1 CoreContext (com.couchbase.client.core.CoreContext)1 Reactor (com.couchbase.client.core.Reactor)1 Stability (com.couchbase.client.core.annotation.Stability)1 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)1 IndividualReplicaGetFailedEvent (com.couchbase.client.core.cnc.events.request.IndividualReplicaGetFailedEvent)1 BucketConfig (com.couchbase.client.core.config.BucketConfig)1 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)1 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)1 CommonExceptions (com.couchbase.client.core.error.CommonExceptions)1 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 DefaultErrorUtil.keyValueStatusToException (com.couchbase.client.core.error.DefaultErrorUtil.keyValueStatusToException)1 DocumentUnretrievableException (com.couchbase.client.core.error.DocumentUnretrievableException)1 AggregateErrorContext (com.couchbase.client.core.error.context.AggregateErrorContext)1 ReducedKeyValueErrorContext (com.couchbase.client.core.error.context.ReducedKeyValueErrorContext)1 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)1 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)1