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

cql branch, Error mapping, IT fix #610

Merged
merged 1 commit into from
Nov 1, 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
@@ -1,7 +1,15 @@
package io.stargate.sgv2.jsonapi.exception.mappers;

import com.datastax.oss.driver.api.core.AllNodesFailedException;
import com.datastax.oss.driver.api.core.DriverException;
import com.datastax.oss.driver.api.core.DriverTimeoutException;
import com.datastax.oss.driver.api.core.NoNodeAvailableException;
import com.datastax.oss.driver.api.core.NodeUnavailableException;
import com.datastax.oss.driver.api.core.servererrors.QueryValidationException;
import com.datastax.oss.driver.api.core.servererrors.WriteTimeoutException;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.quarkus.security.UnauthorizedException;
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import jakarta.ws.rs.core.Response;
Expand All @@ -14,19 +22,18 @@
* implementation.
*/
public final class ThrowableToErrorMapper {

private static final BiFunction<Throwable, String, CommandResult.Error> MAPPER_WITH_MESSAGE =
(throwable, message) -> {
// if our own exception, shortcut
if (throwable instanceof JsonApiException jae) {
return jae.getCommandResultError(message);
}
// Override response error code
// add error code as error field
Map<String, Object> fields = Map.of("exceptionClass", throwable.getClass().getSimpleName());
if (throwable instanceof StatusRuntimeException sre) {
Map<String, Object> fields =
Map.of("exceptionClass", throwable.getClass().getSimpleName());
if (sre.getStatus().getCode() == Status.Code.UNAUTHENTICATED) {
return new CommandResult.Error(message, fields, Response.Status.UNAUTHORIZED);
return new CommandResult.Error(
"UNAUTHENTICATED: Invalid token", fields, Response.Status.UNAUTHORIZED);
} else if (sre.getStatus().getCode() == Status.Code.INTERNAL) {
return new CommandResult.Error(message, fields, Response.Status.INTERNAL_SERVER_ERROR);
} else if (sre.getStatus().getCode() == Status.Code.UNAVAILABLE) {
Expand All @@ -35,18 +42,33 @@ public final class ThrowableToErrorMapper {
return new CommandResult.Error(message, fields, Response.Status.GATEWAY_TIMEOUT);
}
}
// add error code as error field
Map<String, Object> fields = Map.of("exceptionClass", throwable.getClass().getSimpleName());
if (throwable instanceof UnauthorizedException
|| throwable
instanceof com.datastax.oss.driver.api.core.servererrors.UnauthorizedException) {
return new CommandResult.Error(
"UNAUTHENTICATED: Invalid token", fields, Response.Status.UNAUTHORIZED);
} else if (throwable instanceof QueryValidationException) {
if (message.contains("vector<float,")) { // TODO is there a better way?
message = "Mismatched vector dimension";
}
return new CommandResult.Error(message, fields, Response.Status.OK);
} else if (throwable instanceof NodeUnavailableException
|| throwable instanceof DriverException
|| throwable instanceof AllNodesFailedException
|| throwable instanceof NoNodeAvailableException) {
return new CommandResult.Error(message, fields, Response.Status.INTERNAL_SERVER_ERROR);
} else if (throwable instanceof DriverTimeoutException
|| throwable instanceof WriteTimeoutException) {
return new CommandResult.Error(message, fields, Response.Status.GATEWAY_TIMEOUT);
}
return new CommandResult.Error(message, fields, Response.Status.OK);
};

private static final Function<Throwable, CommandResult.Error> MAPPER =
throwable -> {
String message = throwable.getMessage();
if (message == null) {
message = "Unexpected exception occurred.";
}

return MAPPER_WITH_MESSAGE.apply(throwable, message);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ public void regularError() {
""";
AnyOf<String> anyOf =
AnyOf.anyOf(
endsWith(
"INVALID_ARGUMENT: table %s.%s does not exist"
.formatted(namespaceName, "badCollection")),
endsWith("INVALID_ARGUMENT: table %s does not exist".formatted("badCollection")));
endsWith("table %s.%s does not exist".formatted(namespaceName, "badCollection")),
endsWith("table %s does not exist".formatted("badCollection")));
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
Expand All @@ -79,7 +77,7 @@ public void regularError() {
.body("errors", is(notNullValue()))
.body("errors[0].message", is(not(blankString())))
.body("errors[0].message", anyOf)
.body("errors[0].exceptionClass", is("StatusRuntimeException"));
.body("errors[0].exceptionClass", is("InvalidQueryException"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ public void insertVectorWithUnmatchedSize() {
.then()
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors[0].message", endsWith("Expected vector of size 5, but received 3"));
.body("errors[0].message", endsWith("Mismatched vector dimension"));

// Insert data with $vector array size greater than vector index defined size.
final String vectorStrCount7 = buildVectorElements(0, 7);
Expand Down Expand Up @@ -1456,7 +1456,7 @@ public void insertVectorWithUnmatchedSize() {
.then()
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors[0].message", endsWith("Expected vector of size 5, but received 7"));
.body("errors[0].message", endsWith("Mismatched vector dimension"));
}

@Test
Expand Down Expand Up @@ -1486,7 +1486,7 @@ public void findVectorWithUnmatchedSize() {
.then()
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors[0].message", endsWith("Expected vector of size 5, but received 3"));
.body("errors[0].message", endsWith("Mismatched vector dimension"));

// Insert data with $vector array size greater than vector index defined size.
final String vectorStrCount7 = buildVectorElements(0, 7);
Expand All @@ -1512,7 +1512,7 @@ public void findVectorWithUnmatchedSize() {
.then()
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors[0].message", endsWith("Expected vector of size 5, but received 7"));
.body("errors[0].message", endsWith("Mismatched vector dimension"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void authenticationError() {
.singleElement()
.satisfies(
error -> {
assertThat(error.message()).isEqualTo("UNAUTHENTICATED");
assertThat(error.message()).isEqualTo("UNAUTHENTICATED: Invalid token");
assertThat(error.status()).isEqualTo(Response.Status.UNAUTHORIZED);
assertThat(error.fields())
.hasSize(1)
Expand Down