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

(incremental) Catch and re-throw Java CQL Driver fail exceptions as JsonApiException #851

Merged
merged 16 commits into from
Feb 14, 2024
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 @@ -130,7 +130,13 @@ public enum ErrorCode {
VECTORIZE_SERVICE_TYPE_UNAVAILABLE("Vectorize service unavailable : "),
VECTORIZE_USAGE_ERROR("Vectorize search can't be used with other sort clause"),

VECTORIZECONFIG_CHECK_FAIL("Internal server error: VectorizeConfig check fail");
VECTORIZECONFIG_CHECK_FAIL("Internal server error: VectorizeConfig check fail"),

UNAUTHENTICATED_REQUEST("UNAUTHENTICATED: Invalid token"),
INVALID_QUERY("Invalid query"),
DRIVER_TIMEOUT("Driver timeout"),
DRIVER_CLOSED_CONNECTION("Driver request connection is closed"),
NO_NODE_AVAILABLE("No node was available to execute the query");

private final String message;

Expand All @@ -145,4 +151,8 @@ public String getMessage() {
public JsonApiException toApiException(String format, Object... args) {
return new JsonApiException(this, message + ": " + String.format(format, args));
}

public JsonApiException toApiException() {
return new JsonApiException(this, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CommandResult get() {
}

// construct and return
CommandResult.Error error = getCommandResultError(message);
CommandResult.Error error = getCommandResultError(message, Response.Status.OK);

// handle cause as well
Throwable cause = getCause();
Expand All @@ -60,7 +60,7 @@ public CommandResult get() {
}
}

public CommandResult.Error getCommandResultError(String message) {
public CommandResult.Error getCommandResultError(String message, Response.Status status) {
Yuqi-Du marked this conversation as resolved.
Show resolved Hide resolved
Map<String, Object> fieldsForMetricsTag =
Map.of("errorCode", errorCode.name(), "exceptionClass", this.getClass().getSimpleName());
SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
Expand All @@ -72,7 +72,7 @@ public CommandResult.Error getCommandResultError(String message) {
? Map.of(
"errorCode", errorCode.name(), "exceptionClass", this.getClass().getSimpleName())
: Map.of("errorCode", errorCode.name());
return new CommandResult.Error(message, fieldsForMetricsTag, fields, Response.Status.OK);
return new CommandResult.Error(message, fieldsForMetricsTag, fields, status);
}

public ErrorCode getErrorCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
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.auth.AuthenticationException;
import com.datastax.oss.driver.api.core.connection.ClosedConnectionException;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.driver.api.core.servererrors.QueryValidationException;
import com.datastax.oss.driver.api.core.servererrors.ReadTimeoutException;
Expand All @@ -30,71 +32,91 @@ public final class ThrowableToErrorMapper {
(throwable, message) -> {
// if our own exception, shortcut
if (throwable instanceof JsonApiException jae) {
return jae.getCommandResultError(message);
return jae.getCommandResultError(message, Response.Status.OK);
}
// Override response error code

// construct fieldsForMetricsTag, only expose exceptionClass in debugMode
SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
DebugModeConfig debugModeConfig = config.getConfigMapping(DebugModeConfig.class);
final boolean debugEnabled = debugModeConfig.enabled();
Map<String, Object> fields =
debugEnabled ? Map.of("exceptionClass", throwable.getClass().getSimpleName()) : null;
final Map<String, Object> fieldsForMetricsTag =
Map.of("exceptionClass", throwable.getClass().getSimpleName());

// Authentication Failure -> ErrorCode.UNAUTHORIZED_REQUEST
if (throwable instanceof UnauthorizedException
|| throwable
instanceof com.datastax.oss.driver.api.core.servererrors.UnauthorizedException) {
return new CommandResult.Error(
"UNAUTHENTICATED: Invalid token",
fieldsForMetricsTag,
fields,
Response.Status.UNAUTHORIZED);
return ErrorCode.UNAUTHENTICATED_REQUEST
.toApiException()
.getCommandResultError(
ErrorCode.UNAUTHENTICATED_REQUEST.getMessage(), Response.Status.UNAUTHORIZED);
// Driver QueryValidationException -> ErrorCode.INVALID_QUERY
} else if (throwable instanceof QueryValidationException) {
if (message.contains("vector<float,")) { // TODO is there a better way?
if (message.contains("vector<float,")) {
message = "Mismatched vector dimension";
}
fields = Map.of("errorCode", ErrorCode.INVALID_REQUEST.name());
return new CommandResult.Error(message, fieldsForMetricsTag, fields, Response.Status.OK);
return ErrorCode.INVALID_QUERY
.toApiException()
.getCommandResultError(message, Response.Status.OK);
// Driver Timeout Exception -> ErrorCode.OPERATION_TIMEOUT
} else if (throwable instanceof DriverTimeoutException
|| throwable instanceof WriteTimeoutException
|| throwable instanceof ReadTimeoutException) {
return new CommandResult.Error(message, fieldsForMetricsTag, fields, Response.Status.OK);
} else if (throwable instanceof DriverException) {
if (throwable instanceof AllNodesFailedException) {
Map<Node, List<Throwable>> nodewiseErrors =
((AllNodesFailedException) throwable).getAllErrors();
if (!nodewiseErrors.isEmpty()) {
List<Throwable> errors = nodewiseErrors.values().iterator().next();
if (errors != null && !errors.isEmpty()) {
Throwable error =
errors.stream()
.findAny()
.filter(
t ->
t instanceof AuthenticationException
|| t instanceof IllegalArgumentException)
.orElse(null);
// connecting to oss cassandra throws AuthenticationException for invalid
// credentials connecting to AstraDB throws IllegalArgumentException for invalid
// token/credentials
if (error instanceof AuthenticationException
|| (error instanceof IllegalArgumentException
&& (error.getMessage().contains("AUTHENTICATION ERROR")
|| error
.getMessage()
.contains(
"Provided username token and/or password are incorrect")))) {
return new CommandResult.Error(
"UNAUTHENTICATED: Invalid token",
fieldsForMetricsTag,
fields,
Response.Status.UNAUTHORIZED);
}
return ErrorCode.DRIVER_TIMEOUT
Yuqi-Du marked this conversation as resolved.
Show resolved Hide resolved
.toApiException()
.getCommandResultError(message, Response.Status.INTERNAL_SERVER_ERROR);
// Driver AllNodesFailedException a composite exception
// peeling the errors from it
} else if (throwable instanceof AllNodesFailedException) {
Map<Node, List<Throwable>> nodewiseErrors =
((AllNodesFailedException) throwable).getAllErrors();
if (!nodewiseErrors.isEmpty()) {
List<Throwable> errors = nodewiseErrors.values().iterator().next();
if (errors != null && !errors.isEmpty()) {
Throwable error =
errors.stream()
.findAny()
.filter(
t ->
t instanceof AuthenticationException
|| t instanceof IllegalArgumentException
|| t instanceof NoNodeAvailableException)
.orElse(null);
// connect to oss cassandra throws AuthenticationException for invalid credentials
// connect to AstraDB throws IllegalArgumentException for invalid token/credentials
if (error instanceof AuthenticationException
|| (error instanceof IllegalArgumentException
&& (error.getMessage().contains("AUTHENTICATION ERROR")
|| error
.getMessage()
.contains(
"Provided username token and/or password are incorrect")))) {
return ErrorCode.UNAUTHENTICATED_REQUEST
.toApiException()
.getCommandResultError(
ErrorCode.UNAUTHENTICATED_REQUEST.getMessage(),
Response.Status.UNAUTHORIZED);
// Driver NoNodeAvailableException -> ErrorCode.NO_NODE_AVAILABLE
} else if (error instanceof NoNodeAvailableException) {
return ErrorCode.NO_NODE_AVAILABLE
.toApiException()
.getCommandResultError(message, Response.Status.INTERNAL_SERVER_ERROR);
}
}
}
// Driver ClosedConnectionException -> ErrorCode.DRIVER_CLOSED_CONNECTION
} else if (throwable instanceof ClosedConnectionException) {
return ErrorCode.DRIVER_CLOSED_CONNECTION
.toApiException()
.getCommandResultError(message, Response.Status.INTERNAL_SERVER_ERROR);
// Unidentified Driver Exceptions, will not mapper into JsonApiException
} else if (throwable instanceof DriverException) {
return new CommandResult.Error(
message, fieldsForMetricsTag, fields, Response.Status.INTERNAL_SERVER_ERROR);
}

return new CommandResult.Error(message, fieldsForMetricsTag, fields, Response.Status.OK);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ public void failWithZerosVector() {
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors", hasSize(1))
.body("errors[0].errorCode", is("INVALID_REQUEST"))
.body("errors[0].errorCode", is("INVALID_QUERY"))
.body(
"errors[0].message",
endsWith("Zero vectors cannot be indexed or queried with cosine similarity"));
Expand Down
Loading