Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error when the namespace doesn't exist #557

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import com.google.protobuf.ByteString;
import com.google.protobuf.BytesValue;
import com.google.protobuf.Int32Value;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.smallrye.mutiny.Uni;
import io.stargate.bridge.proto.QueryOuterClass;
import io.stargate.bridge.proto.Schema;
import io.stargate.sgv2.api.common.StargateRequestInfo;
import io.stargate.sgv2.api.common.config.QueriesConfig;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.Base64;
Expand Down Expand Up @@ -120,13 +124,25 @@ protected Uni<Optional<Schema.CqlTable>> getSchema(String namespace, String coll
final Uni<Schema.CqlKeyspaceDescribe> cqlKeyspaceDescribeUni =
stargateRequestInfo.getStargateBridge().describeKeyspace(describeKeyspaceQuery);
return cqlKeyspaceDescribeUni
.onItem()
.transform(
cqlKeyspaceDescribe -> {
.onItemOrFailure()
.transformToUni(
(cqlKeyspaceDescribe, error) -> {
if (error != null
&& (error instanceof StatusRuntimeException sre
&& sre.getStatus().getCode() == Status.Code.NOT_FOUND)) {
return Uni.createFrom()
.failure(
new RuntimeException(
new JsonApiException(
ErrorCode.NAMESPACE_DOES_NOT_EXIST,
"The provided namespace does not exist: " + namespace)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to improve the following error that happens when you call find() on a collection that doesn't exist?

Error: Command "find" failed with the following errors: [{"message":"INVALID_ARGUMENT: table articles22 does not exist","exceptionClass":"StatusRuntimeException"}]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vkarpov15 I think this is about namespace, not collection (table)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means to improve the error when the namespace doesn't exist, not the collection. I guess you got the error when you input the wrong collection name (or table showed in the error message).

}
Schema.CqlTable cqlTable = null;
return cqlKeyspaceDescribe.getTablesList().stream()
.filter(table -> table.getName().equals(collectionName))
.findFirst();
return Uni.createFrom()
.item(
cqlKeyspaceDescribe.getTablesList().stream()
.filter(table -> table.getName().equals(collectionName))
.findFirst());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ private void insert(String json) {
.statusCode(200);
}

@Test
public void wrongNamespace() {
String json =
"""
{
"find": {
"sort" : {"$vector" : [0.15, 0.1, 0.1, 0.35, 0.55]},
"options" : {
"limit" : 100
}
}
}
""";
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, "something_else", collectionName)
.then()
.statusCode(200)
.body("status", is(nullValue()))
.body("data", is(nullValue()))
.body("errors[0].message", is("The provided namespace does not exist: something_else"))
.body("errors[0].errorCode", is("NAMESPACE_DOES_NOT_EXIST"))
.body("errors[0].exceptionClass", is("JsonApiException"));
}

@Test
public void noFilter() {
String json = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static org.hamcrest.Matchers.blankString;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
Expand Down Expand Up @@ -751,13 +750,12 @@ public void orderedDuplicateDocumentNoNamespace() {
.post(CollectionResource.BASE_PATH, "something_else", collectionName)
.then()
.statusCode(200)
.body("status.insertedIds", is(empty()))
.body("status.insertedIds", is(nullValue()))
.body("data", is(nullValue()))
.body(
"errors[0].message",
startsWith(
"Failed to insert document with _id 'doc4': INVALID_ARGUMENT: keyspace something_else does not exist"))
.body("errors[0].exceptionClass", is("StatusRuntimeException"));
startsWith("The provided namespace does not exist: something_else"))
.body("errors[0].exceptionClass", is("JsonApiException"));
}

@Test
Expand Down