diff --git a/src/main/java/io/stargate/sgv2/jsonapi/api/exception/WebApplicationExceptionMapper.java b/src/main/java/io/stargate/sgv2/jsonapi/api/exception/WebApplicationExceptionMapper.java new file mode 100644 index 0000000000..d9ce2e075b --- /dev/null +++ b/src/main/java/io/stargate/sgv2/jsonapi/api/exception/WebApplicationExceptionMapper.java @@ -0,0 +1,18 @@ +package io.stargate.sgv2.jsonapi.api.exception; + +import io.stargate.sgv2.jsonapi.api.model.command.CommandResult; +import io.stargate.sgv2.jsonapi.exception.mappers.ThrowableCommandResultSupplier; +import javax.ws.rs.WebApplicationException; +import org.jboss.resteasy.reactive.RestResponse; +import org.jboss.resteasy.reactive.server.ServerExceptionMapper; + +/** Tries to omit the `WebApplicationException` and just report the cause. */ +public class WebApplicationExceptionMapper { + + @ServerExceptionMapper + public RestResponse genericExceptionMapper(WebApplicationException e) { + Throwable toReport = null != e.getCause() ? e.getCause() : e; + CommandResult commandResult = new ThrowableCommandResultSupplier(toReport).get(); + return RestResponse.ok(commandResult); + } +} diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/CollectionResourceIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/CollectionResourceIntegrationTest.java index 7a2faceda3..c3bc84ae73 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/CollectionResourceIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/CollectionResourceIntegrationTest.java @@ -60,9 +60,7 @@ public void malformedBody() { .then() .statusCode(200) .body("errors[0].message", is(not(blankString()))) - .body("errors[0].exceptionClass", is("WebApplicationException")) - .body("errors[1].message", is(not(blankString()))) - .body("errors[1].exceptionClass", is("JsonParseException")); + .body("errors[0].exceptionClass", is("JsonParseException")); } @Test diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/GeneralResourceIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/GeneralResourceIntegrationTest.java index 1d06070ad5..f5b51e5274 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/GeneralResourceIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/GeneralResourceIntegrationTest.java @@ -90,18 +90,18 @@ public final void alreadyExists() { public final void withReplicationFactor() { String json = """ - { - "createNamespace": { - "name": "%s", - "options": { - "replication": { - "class": "SimpleStrategy", - "replication_factor": 2 - } - } + { + "createNamespace": { + "name": "%s", + "options": { + "replication": { + "class": "SimpleStrategy", + "replication_factor": 2 } } - """ + } + } + """ .formatted(DB_NAME); given() @@ -119,11 +119,11 @@ public final void withReplicationFactor() { public void invalidCommand() { String json = """ - { - "createNamespace": { - } - } - """; + { + "createNamespace": { + } + } + """; given() .header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken()) @@ -167,20 +167,18 @@ public void malformedBody() { .then() .statusCode(200) .body("errors[0].message", is(not(blankString()))) - .body("errors[0].exceptionClass", is("WebApplicationException")) - .body("errors[1].message", is(not(blankString()))) - .body("errors[1].exceptionClass", is("JsonParseException")); + .body("errors[0].exceptionClass", is("JsonParseException")); } @Test public void unknownCommand() { String json = """ - { - "unknownCommand": { - } - } - """; + { + "unknownCommand": { + } + } + """; given() .header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken()) diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/NamespaceResourceIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/NamespaceResourceIntegrationTest.java index 3ee7a8f35d..0a324db404 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/NamespaceResourceIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/NamespaceResourceIntegrationTest.java @@ -85,9 +85,7 @@ public void malformedBody() { .then() .statusCode(200) .body("errors[0].message", is(not(blankString()))) - .body("errors[0].exceptionClass", is("WebApplicationException")) - .body("errors[1].message", is(not(blankString()))) - .body("errors[1].exceptionClass", is("JsonParseException")); + .body("errors[0].exceptionClass", is("JsonParseException")); } @Test