use of com.couchbase.client.core.error.CouchbaseException in project couchbase-jvm-clients by couchbase.
the class SubdocGetRequest method decode.
@Override
public SubdocGetResponse decode(final ByteBuf response, KeyValueChannelContext ctx) {
Optional<ByteBuf> maybeBody = body(response);
SubDocumentField[] values;
List<CouchbaseException> errors = null;
if (maybeBody.isPresent()) {
ByteBuf body = maybeBody.get();
values = new SubDocumentField[commands.size()];
for (Command command : commands) {
short statusRaw = body.readShort();
SubDocumentOpResponseStatus status = decodeSubDocumentStatus(statusRaw);
Optional<CouchbaseException> error = Optional.empty();
if (status != SubDocumentOpResponseStatus.SUCCESS) {
if (errors == null)
errors = new ArrayList<>();
CouchbaseException err = mapSubDocumentError(this, status, command.path, command.originalIndex());
errors.add(err);
error = Optional.of(err);
}
int valueLength = body.readInt();
byte[] value = new byte[valueLength];
body.readBytes(value, 0, valueLength);
SubDocumentField op = new SubDocumentField(status, error, value, command.path, command.type);
values[command.originalIndex] = op;
}
} else {
values = new SubDocumentField[0];
}
short rawStatus = status(response);
ResponseStatus status = decodeStatus(response);
boolean isDeleted = rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE_DELETED.status() || rawStatus == Status.SUBDOC_SUCCESS_DELETED_DOCUMENT.status();
Optional<CouchbaseException> error = Optional.empty();
// Note that we send all subdoc requests as multi currently so always get this back on error
if (rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE.status() || rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE_DELETED.status()) {
// Special case logic for CMD_EXISTS
if (commands.size() == 1 && commands.get(0).type == SubdocCommandType.EXISTS) {
status = ResponseStatus.SUCCESS;
} else // If a single subdoc op was tried and failed, return that directly
if (commands.size() == 1 && errors != null && errors.size() == 1) {
error = Optional.of(errors.get(0));
} else {
// Otherwise return success, as some of the operations have succeeded
status = ResponseStatus.SUCCESS;
}
}
// Handle any document-level failures here
if (rawStatus == Status.SUBDOC_DOC_NOT_JSON.status()) {
SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.DOC_NOT_JSON);
error = Optional.of(new DocumentNotJsonException(e));
} else if (rawStatus == Status.SUBDOC_DOC_TOO_DEEP.status()) {
SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.DOC_TOO_DEEP);
error = Optional.of(new DocumentTooDeepException(e));
} else if (rawStatus == Status.SUBDOC_XATTR_INVALID_KEY_COMBO.status()) {
SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.XATTR_INVALID_KEY_COMBO);
error = Optional.of(new XattrInvalidKeyComboException(e));
}
// Do not handle SUBDOC_INVALID_COMBO here, it indicates a client-side bug
return new SubdocGetResponse(status, error, values, cas(response), isDeleted);
}
use of com.couchbase.client.core.error.CouchbaseException in project couchbase-jvm-clients by couchbase.
the class SubdocMutateIntegrationTest method createAsDeletedCanAccess.
@Test
@IgnoreWhen(missesCapabilities = Capabilities.CREATE_AS_DELETED, clusterTypes = ClusterType.CAVES)
void createAsDeletedCanAccess() {
String docId = docId();
try {
coll.mutateIn(docId, Collections.singletonList(MutateInSpec.insert("foo", "bar").xattr()), MutateInOptions.mutateInOptions().createAsDeleted(true).storeSemantics(StoreSemantics.INSERT));
assertThrows(DocumentNotFoundException.class, () -> coll.get(docId));
assertThrows(DocumentNotFoundException.class, () -> coll.lookupIn(docId, Collections.singletonList(LookupInSpec.get("foo").xattr())));
LookupInResult result = coll.lookupIn(docId, Collections.singletonList(LookupInSpec.get("foo").xattr()), LookupInOptions.lookupInOptions().accessDeleted(true));
assertEquals("bar", result.contentAs(0, String.class));
} catch (CouchbaseException err) {
// createAsDeleted flag only supported in 6.5.1+
assertEquals(ResponseStatus.INVALID_REQUEST, err.context().responseStatus());
}
}
use of com.couchbase.client.core.error.CouchbaseException in project couchbase-jvm-clients by couchbase.
the class QueryIntegrationTest method readOnlyViolation.
@Test
void readOnlyViolation() {
QueryOptions options = queryOptions().readonly(true);
CouchbaseException e = assertThrows(CouchbaseException.class, () -> cluster.query("INSERT INTO " + bucketName + " (KEY, VALUE) values (\"foo\", \"bar\")", options));
assertEquals(1000, ((QueryErrorContext) e.context()).errors().get(0).code());
}
use of com.couchbase.client.core.error.CouchbaseException in project couchbase-jvm-clients by couchbase.
the class SubdocMutateIntegrationTest method reviveDocumentWithoutAccessDeleted.
@IgnoreWhen(missesCapabilities = { Capabilities.SUBDOC_REVIVE_DOCUMENT })
@Test
void reviveDocumentWithoutAccessDeleted() {
String docId = docId();
JsonObject body = JsonObject.create().put("foo", "bar");
coll.insert(docId, body);
try {
coll.mutateIn(docId, Collections.singletonList(upsert("foo", "bar").xattr()), MutateInOptions.mutateInOptions().storeSemantics(StoreSemantics.REVIVE));
fail();
} catch (CouchbaseException err) {
// "ReviveDocument can’t be used without AccessDeleted"
}
}
use of com.couchbase.client.core.error.CouchbaseException in project couchbase-jvm-clients by couchbase.
the class SubdocMutateIntegrationTest method createAsDeletedCanInsertOnTop.
@Test
@IgnoreWhen(missesCapabilities = Capabilities.CREATE_AS_DELETED, clusterTypes = ClusterType.CAVES)
void createAsDeletedCanInsertOnTop() {
String docId = docId();
try {
coll.mutateIn(docId, Collections.singletonList(MutateInSpec.insert("foo", "bar").xattr()), MutateInOptions.mutateInOptions().createAsDeleted(true).storeSemantics(StoreSemantics.INSERT));
coll.mutateIn(docId, Collections.singletonList(MutateInSpec.insert("foo", "bar").xattr()), MutateInOptions.mutateInOptions().storeSemantics(StoreSemantics.INSERT));
LookupInResult result = coll.lookupIn(docId, Collections.singletonList(LookupInSpec.get("foo").xattr()));
assertEquals("bar", result.contentAs(0, String.class));
} catch (CouchbaseException err) {
// createAsDeleted flag only supported in 6.5.1+
assertEquals(ResponseStatus.INVALID_REQUEST, err.context().responseStatus());
}
}
Aggregations