Search in sources :

Example 1 with DocumentNotFoundException

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

the class QueryChunkResponseParser method errorsToThrowable.

static CouchbaseException errorsToThrowable(final byte[] bytes, HttpResponse header, RequestContext ctx) {
    int httpStatus = header != null ? header.status().code() : 0;
    final List<ErrorCodeAndMessage> errors = bytes.length == 0 ? Collections.emptyList() : ErrorCodeAndMessage.fromJsonArray(bytes);
    QueryErrorContext errorContext = new QueryErrorContext(ctx, errors, httpStatus);
    if (errors.size() >= 1) {
        ErrorCodeAndMessage codeAndMessage = errors.get(0);
        int code = codeAndMessage.code();
        String message = codeAndMessage.message();
        int reasonCode = codeAndMessage.reason() != null ? (int) codeAndMessage.reason().getOrDefault("code", 0) : 0;
        if (code == 3000) {
            return new ParsingFailureException(errorContext);
        } else if (PREPARED_ERROR_CODES.contains(code)) {
            return new PreparedStatementFailureException(errorContext, RETRYABLE_PREPARED_ERROR_CODES.contains(code));
        } else if (code == 4300 && message.matches("^.*index .*already exist.*")) {
            return new IndexExistsException(errorContext);
        } else if (code >= 4000 && code < 5000) {
            return new PlanningFailureException(errorContext);
        } else if (code == 12004 || code == 12016 || (code == 5000 && message.matches("^.*index .+ not found.*"))) {
            return new IndexNotFoundException(errorContext);
        } else if (code == 5000 && message.matches("^.*Index .*already exist.*")) {
            return new IndexExistsException(errorContext);
        } else if (code == 5000 && message.contains("limit for number of indexes that can be created per scope has been reached")) {
            return new QuotaLimitedException(errorContext);
        } else if (code >= 5000 && code < 6000) {
            return new InternalServerFailureException(errorContext);
        } else if (code == 12009) {
            if (message.contains("CAS mismatch") || reasonCode == 12033) {
                return new CasMismatchException(errorContext);
            } else if (reasonCode == 17014) {
                return new DocumentNotFoundException(errorContext);
            } else if (reasonCode == 17012) {
                return new DocumentExistsException(errorContext);
            } else {
                return new DmlFailureException(errorContext);
            }
        } else if ((code >= 10000 && code < 11000) || code == 13014) {
            return new AuthenticationFailureException("Could not authenticate query", errorContext, null);
        } else if ((code >= 12000 && code < 13000) || (code >= 14000 && code < 15000)) {
            return new IndexFailureException(errorContext);
        } else if (code == 1065) {
            if (message.contains("query_context")) {
                return FeatureNotAvailableException.scopeLevelQuery(ServiceType.QUERY);
            }
            if (message.contains("preserve_expiry")) {
                return FeatureNotAvailableException.queryPreserveExpiry();
            }
        } else if (code == 1080) {
            // engine will proactively send us a timeout and we need to convert it.
            return new UnambiguousTimeoutException("Query timed out while streaming/receiving rows", new CancellationErrorContext(errorContext));
        } else if (code == 1191 || code == 1192 || code == 1193 || code == 1194) {
            return new RateLimitedException(errorContext);
        } else if (code == 3230) {
            String feature = null;
            if (message.contains("Advisor") || message.contains("Advise")) {
                feature = "Query Index Advisor";
            } else if (message.contains("Window")) {
                feature = "Query Window Functions";
            }
            return FeatureNotAvailableException.communityEdition(feature);
        }
    }
    return new CouchbaseException("Unknown query error", errorContext);
}
Also used : IndexExistsException(com.couchbase.client.core.error.IndexExistsException) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) DocumentExistsException(com.couchbase.client.core.error.DocumentExistsException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) PlanningFailureException(com.couchbase.client.core.error.PlanningFailureException) UnambiguousTimeoutException(com.couchbase.client.core.error.UnambiguousTimeoutException) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) QueryErrorContext(com.couchbase.client.core.error.context.QueryErrorContext) QuotaLimitedException(com.couchbase.client.core.error.QuotaLimitedException) ErrorCodeAndMessage(com.couchbase.client.core.error.ErrorCodeAndMessage) ParsingFailureException(com.couchbase.client.core.error.ParsingFailureException) PreparedStatementFailureException(com.couchbase.client.core.error.PreparedStatementFailureException) IndexNotFoundException(com.couchbase.client.core.error.IndexNotFoundException) DmlFailureException(com.couchbase.client.core.error.DmlFailureException) IndexFailureException(com.couchbase.client.core.error.IndexFailureException) CancellationErrorContext(com.couchbase.client.core.error.context.CancellationErrorContext) RateLimitedException(com.couchbase.client.core.error.RateLimitedException) InternalServerFailureException(com.couchbase.client.core.error.InternalServerFailureException)

Example 2 with DocumentNotFoundException

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

the class OpenTelemetryIntegrationTest method capturesTraceSpans.

@Test
void capturesTraceSpans() {
    Span parent = otelTesting.getOpenTelemetry().getTracer("integrationtest").spanBuilder("test").setSpanKind(SpanKind.SERVER).startSpan();
    try (Scope ignored = parent.makeCurrent()) {
        collection.get("myid");
    } catch (DocumentNotFoundException ignored) {
    // expected
    } finally {
        parent.end();
    }
    waitUntilCondition(() -> {
        otelTesting.assertTraces().hasTracesSatisfyingExactly(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("test").hasKind(SpanKind.SERVER), span -> span.hasName("get").hasParentSpanId(parent.getSpanContext().getSpanId()).hasKind(SpanKind.INTERNAL).hasAttributesSatisfying(attributes -> assertThat(attributes).containsEntry("db.system", "couchbase").containsEntry("db.operation", "get").containsEntry("db.couchbase.service", "kv").containsEntry("db.couchbase.collection", "_default").containsEntry("db.couchbase.scope", "_default")), span -> span.hasName("dispatch_to_server").hasKind(SpanKind.INTERNAL).hasAttributesSatisfying(attributes -> assertThat(attributes).containsEntry("db.system", "couchbase"))));
        return true;
    });
}
Also used : OpenTelemetryExtension(io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension) ClusterOptions.clusterOptions(com.couchbase.client.java.ClusterOptions.clusterOptions) Util.waitUntilCondition(com.couchbase.client.test.Util.waitUntilCondition) Span(io.opentelemetry.api.trace.Span) Scope(io.opentelemetry.context.Scope) ClusterAwareIntegrationTest(com.couchbase.client.test.ClusterAwareIntegrationTest) SpanKind(io.opentelemetry.api.trace.SpanKind) Services(com.couchbase.client.test.Services) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) Collection(com.couchbase.client.java.Collection) Bucket(com.couchbase.client.java.Bucket) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) OpenTelemetryAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat) Cluster(com.couchbase.client.java.Cluster) Duration(java.time.Duration) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) Scope(io.opentelemetry.context.Scope) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) Span(io.opentelemetry.api.trace.Span) ClusterAwareIntegrationTest(com.couchbase.client.test.ClusterAwareIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with DocumentNotFoundException

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

the class CouchbaseArrayList method set.

@Override
public E set(int index, E element) {
    // fail fast on negative values, as they are interpreted as "starting from the back of the array" otherwise
    if (index < 0) {
        throw new IndexOutOfBoundsException("Index: " + index);
    }
    String idx = "[" + index + "]";
    for (int i = 0; i < arrayListOptions.casMismatchRetries(); i++) {
        try {
            LookupInResult current = collection.lookupIn(id, Collections.singletonList(LookupInSpec.get(idx)), lookupInOptions);
            long returnCas = current.cas();
            // this loop ensures we return exactly what we replaced
            final E result = current.contentAs(0, entityTypeClass);
            collection.mutateIn(id, Collections.singletonList(MutateInSpec.replace(idx, element)), arrayListOptions.mutateInOptions().cas(returnCas));
            return result;
        } catch (DocumentNotFoundException e) {
            createEmptyList();
        } catch (CasMismatchException ex) {
        // will need to retry get-and-set
        } catch (PathNotFoundException ex) {
            throw new IndexOutOfBoundsException("Index: " + index);
        }
    }
    throw new CouchbaseException("CouchbaseArrayList set failed", new RetryExhaustedException("Couldn't perform set in less than " + arrayListOptions.casMismatchRetries() + " iterations. It is likely concurrent modifications of this document are the reason"));
}
Also used : LookupInResult(com.couchbase.client.java.kv.LookupInResult) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) PathNotFoundException(com.couchbase.client.core.error.subdoc.PathNotFoundException)

Example 4 with DocumentNotFoundException

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

the class Get method main.

public static void main(String... args) {
    /*
     * Connect to the cluster and open a collection to work with.
     */
    Cluster cluster = Cluster.connect("127.0.0.1", "Administrator", "password");
    Bucket bucket = cluster.bucket("travel-sample");
    Collection collection = bucket.defaultCollection();
    try {
        System.out.println("Found Airport: " + collection.get("airport_1291"));
    } catch (DocumentNotFoundException ex) {
        System.out.println("Airport not found!");
    }
    // -------------------------------------------------------------------------------------
    /*
     * If only a couple fields of a document are needed (a projection), then this can be provided
     * through the options.
     */
    GetResult airline = collection.get("airline_10", getOptions().project("airportname", "country"));
    System.out.println("Found airline with fields: " + airline);
    // -------------------------------------------------------------------------------------
    /*
     * In the previous examples, the expiration field has always been empty on the GetResult.
     * If you need to fetch the expiration time of a document, you can also pass it via the
     * options.
     */
    GetResult airline2 = collection.get("airline_10", getOptions().withExpiry(true));
    System.out.println("Expiration is: " + airline2.expiry());
}
Also used : DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) GetResult(com.couchbase.client.java.kv.GetResult) Bucket(com.couchbase.client.java.Bucket) Cluster(com.couchbase.client.java.Cluster) Collection(com.couchbase.client.java.Collection)

Example 5 with DocumentNotFoundException

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

the class KeyValueErrorIntegrationTest method verifyUnlockExceptions.

@Test
void verifyUnlockExceptions() {
    assertThrows(InvalidArgumentException.class, () -> collection.unlock(null, 0));
    assertThrows(InvalidArgumentException.class, () -> collection.unlock("foo", 0));
    assertThrows(InvalidArgumentException.class, () -> collection.unlock("foo", 0, null));
    DocumentNotFoundException thrown = assertThrows(DocumentNotFoundException.class, () -> collection.unlock(UUID.randomUUID().toString(), 1));
    assertNotNull(thrown.context());
}
Also used : DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)19 Test (org.junit.jupiter.api.Test)10 CasMismatchException (com.couchbase.client.core.error.CasMismatchException)8 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)8 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)7 RetryExhaustedException (com.couchbase.client.core.retry.reactor.RetryExhaustedException)6 PathNotFoundException (com.couchbase.client.core.error.subdoc.PathNotFoundException)5 GetResult (com.couchbase.client.java.kv.GetResult)5 LookupInResult (com.couchbase.client.java.kv.LookupInResult)5 JsonObject (com.couchbase.client.java.json.JsonObject)4 MutationResult (com.couchbase.client.java.kv.MutationResult)3 Bucket (com.couchbase.client.java.Bucket)2 Cluster (com.couchbase.client.java.Cluster)2 Collection (com.couchbase.client.java.Collection)2 JsonArray (com.couchbase.client.java.json.JsonArray)2 ClusterAwareIntegrationTest (com.couchbase.client.test.ClusterAwareIntegrationTest)2 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)2 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)1 DmlFailureException (com.couchbase.client.core.error.DmlFailureException)1 DocumentExistsException (com.couchbase.client.core.error.DocumentExistsException)1