Skip to content

Commit

Permalink
Fixes #1200: add handling for invalid JSON requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tatu-at-datastax committed Jun 28, 2024
1 parent b211b21 commit c00e071
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ public enum ErrorCode {

FILTER_FIELDS_LIMIT_VIOLATION("Filter fields size limitation violated"),

/** note: Only used by EmbeddingGateway */
INVALID_REQUEST("Request not supported by the data store"),

INVALID_REQUEST_NOT_JSON("Request invalid, cannot parse as JSON"),

INVALID_INDEXING_DEFINITION("Invalid indexing definition"),

UNINDEXED_FILTER_PATH("Unindexed filter path"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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.*;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParseException;
import io.quarkus.security.UnauthorizedException;
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
Expand Down Expand Up @@ -38,6 +40,11 @@ public final class ThrowableToErrorMapper {
return jae.getCommandResultError(message, Response.Status.OK);
}

// General Exception by Jackson
if (throwable instanceof JacksonException jacksonE) {
return handleJsonProcessingException(jacksonE, message);
}

// UnauthorizedException from quarkus
if (throwable instanceof UnauthorizedException) {
return ErrorCode.UNAUTHENTICATED_REQUEST
Expand Down Expand Up @@ -180,6 +187,19 @@ private static CommandResult.Error handleAllNodesFailedException(
return handleUnrecognizedException(throwable, message);
}

private static CommandResult.Error handleJsonProcessingException(
JacksonException e, String message) {
// Low-level parsing problem?
if (e instanceof JsonParseException) {
return ErrorCode.INVALID_REQUEST_NOT_JSON
.toApiException("underlying problem: (%s) %s", e.getClass().getName(), message)
.getCommandResultError(Response.Status.OK);
}

// Will need to add more handling
return handleUnrecognizedException(e, message);
}

private static CommandResult.Error handleUnrecognizedException(
Throwable throwable, String message) {
logger.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ public void malformedBody() {
given()
.headers(getHeaders())
.contentType(ContentType.JSON)
.body("{wrong}")
.body("wrong")
.when()
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
.statusCode(200)
.body("errors", hasSize(1))
.body("errors[0].errorCode", is("INVALID_REQUEST_NOT_JSON"))
.body("errors[0].exceptionClass", is("JsonApiException"))
.body("errors[0].message", is(not(blankString())));
.body("errors[0].message", is("acb"));
// .body("errors[0].message", is(not(blankString())));
}

@Test
Expand All @@ -69,7 +72,9 @@ public void unknownCommand() {
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
.statusCode(200)
.body("errors", hasSize(1))
.body("errors[0].errorCode", is("NO_COMMAND_MATCHED"))
.body("errors[0].exceptionClass", is("JsonApiException"))
.body(
"errors[0].message",
startsWith("No \"unknownCommand\" command found as \"CollectionCommand\""));
Expand All @@ -94,6 +99,7 @@ public void invalidNamespaceName() {
.post(CollectionResource.BASE_PATH, "7_no_leading_number", collectionName)
.then()
.statusCode(200)
.body("errors", hasSize(1))
.body("errors[0].errorCode", is("COMMAND_FIELD_INVALID"))
.body("errors[0].exceptionClass", is("JsonApiException"))
.body(
Expand Down Expand Up @@ -121,6 +127,7 @@ public void invalidCollectionName() {
.post(CollectionResource.BASE_PATH, namespaceName, "7_no_leading_number")
.then()
.statusCode(200)
.body("errors", hasSize(1))
.body("errors[0].errorCode", is("COMMAND_FIELD_INVALID"))
.body("errors[0].exceptionClass", is("JsonApiException"))
.body(
Expand All @@ -138,6 +145,7 @@ public void emptyBody() {
.post(CollectionResource.BASE_PATH, namespaceName, collectionName)
.then()
.statusCode(200)
.body("errors", hasSize(1))
.body("errors[0].errorCode", is("COMMAND_FIELD_INVALID"))
.body("errors[0].exceptionClass", is("JsonApiException"))
.body(
Expand Down

0 comments on commit c00e071

Please sign in to comment.