Skip to content

Commit

Permalink
closes #350: errorCode and exceptionClass to be reported always with …
Browse files Browse the repository at this point in the history
…JsonApiException command result
  • Loading branch information
Ivan Senic committed Apr 18, 2023
1 parent e4811dc commit fb302f2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ public CommandResult get() {
message = errorCode.getMessage();
}

// add error code as error field
Map<String, Object> fields = Map.of("errorCode", errorCode.name());

// construct and return
CommandResult.Error error = new CommandResult.Error(message, fields);
CommandResult.Error error = getCommandResultError(message);

// handle cause as well
Throwable cause = getCause();
Expand All @@ -59,6 +56,12 @@ public CommandResult get() {
}
}

public CommandResult.Error getCommandResultError(String message) {
Map<String, Object> fields =
Map.of("errorCode", errorCode.name(), "exceptionClass", this.getClass().getSimpleName());
return new CommandResult.Error(message, fields);
}

public ErrorCode getErrorCode() {
return errorCode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.stargate.sgv2.jsonapi.exception.mappers;

import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
Expand All @@ -10,21 +12,35 @@
*/
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);
}

// add error code as error field
Map<String, Object> fields = Map.of("exceptionClass", throwable.getClass().getSimpleName());
return new CommandResult.Error(message, fields);
};

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

// add error code as error field
Map<String, Object> fields = Map.of("exceptionClass", throwable.getClass().getSimpleName());
return new CommandResult.Error(message, fields);
return MAPPER_WITH_MESSAGE.apply(throwable, message);
};

private ThrowableToErrorMapper() {}

public static Function<Throwable, CommandResult.Error> getMapperFunction() {
return MAPPER;
}

public static BiFunction<Throwable, String, CommandResult.Error> getMapperWithMessageFunction() {
return MAPPER_WITH_MESSAGE;
}
}
18 changes: 7 additions & 11 deletions src/main/java/io/stargate/sgv2/jsonapi/util/ExceptionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@

import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import io.stargate.sgv2.jsonapi.exception.mappers.ThrowableToErrorMapper;
import io.stargate.sgv2.jsonapi.service.shredding.model.DocumentId;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExceptionUtil {
public static String getThrowableGroupingKey(Throwable error) {
String key = error.getClass().getSimpleName();
if (error instanceof JsonApiException jae) key = jae.getErrorCode().name();
return key;
if (error instanceof JsonApiException jae) {
return jae.getErrorCode().name();
} else {
return error.getClass().getSimpleName();
}
}

public static CommandResult.Error getError(
String messageTemplate, List<DocumentId> documentIds, Throwable throwable) {
String message = messageTemplate.formatted(documentIds, throwable.getMessage());
Map<String, Object> fields = new HashMap<>();
fields.put("exceptionClass", throwable.getClass().getSimpleName());
if (throwable instanceof JsonApiException jae) {
fields.put("errorCode", jae.getErrorCode().name());
}
return new CommandResult.Error(message, fields);
return ThrowableToErrorMapper.getMapperWithMessageFunction().apply(throwable, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ public void inConditionEmptyArray() {
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors[1].message", is("$in operator must have at least one value"))
.body("errors[1].exceptionClass", is("JsonApiException"));
.body("errors[1].exceptionClass", is("JsonApiException"))
.body("errors[1].errorCode", is("INVALID_FILTER_EXPRESSION"));
}

@Test
Expand All @@ -300,7 +301,8 @@ public void inConditionNonArrayArray() {
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors[1].message", is("$in operator must have `ARRAY`"))
.body("errors[1].exceptionClass", is("JsonApiException"));
.body("errors[1].exceptionClass", is("JsonApiException"))
.body("errors[1].errorCode", is("INVALID_FILTER_EXPRESSION"));
}

@Test
Expand All @@ -323,7 +325,8 @@ public void inConditionNonIdField() {
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors[1].message", is("Can use $in operator only on _id field"))
.body("errors[1].exceptionClass", is("JsonApiException"));
.body("errors[1].exceptionClass", is("JsonApiException"))
.body("errors[1].errorCode", is("INVALID_FILTER_EXPRESSION"));
}

@Test
Expand Down Expand Up @@ -512,7 +515,8 @@ public void withExistFalseOperator() {
.then()
.statusCode(200)
.body("errors[1].message", is("$exists operator supports only true"))
.body("errors[1].exceptionClass", is("JsonApiException"));
.body("errors[1].exceptionClass", is("JsonApiException"))
.body("errors[1].errorCode", is("INVALID_FILTER_EXPRESSION"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ public void inConditionEmptyArray() {
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors[1].message", is("$in operator must have at least one value"))
.body("errors[1].exceptionClass", is("JsonApiException"));
.body("errors[1].exceptionClass", is("JsonApiException"))
.body("errors[1].errorCode", is("INVALID_FILTER_EXPRESSION"));
}

@Test
Expand All @@ -330,7 +331,8 @@ public void inConditionNonArrayArray() {
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors[1].message", is("$in operator must have `ARRAY`"))
.body("errors[1].exceptionClass", is("JsonApiException"));
.body("errors[1].exceptionClass", is("JsonApiException"))
.body("errors[1].errorCode", is("INVALID_FILTER_EXPRESSION"));
}

@Test
Expand All @@ -353,7 +355,8 @@ public void inConditionNonIdField() {
.statusCode(200)
.body("errors", is(notNullValue()))
.body("errors[1].message", is("Can use $in operator only on _id field"))
.body("errors[1].exceptionClass", is("JsonApiException"));
.body("errors[1].exceptionClass", is("JsonApiException"))
.body("errors[1].errorCode", is("INVALID_FILTER_EXPRESSION"));
}

@Test
Expand Down Expand Up @@ -535,7 +538,8 @@ public void withExistsOperatorFalse() {
.body("data", is(nullValue()))
.body("status", is(nullValue()))
.body("errors[1].message", is("$exists operator supports only true"))
.body("errors[1].exceptionClass", is("JsonApiException"));
.body("errors[1].exceptionClass", is("JsonApiException"))
.body("errors[1].errorCode", is("INVALID_FILTER_EXPRESSION"));
}

@Test
Expand Down Expand Up @@ -635,7 +639,8 @@ public void withAllOperatorNotArray() {
.body("data", is(nullValue()))
.body("status", is(nullValue()))
.body("errors[1].message", is("$all operator must have `ARRAY` value"))
.body("errors[1].exceptionClass", is("JsonApiException"));
.body("errors[1].exceptionClass", is("JsonApiException"))
.body("errors[1].errorCode", is("INVALID_FILTER_EXPRESSION"));
}

@Test
Expand Down Expand Up @@ -711,7 +716,8 @@ public void withSizeOperatorNotNumber() {
.body("data", is(nullValue()))
.body("status", is(nullValue()))
.body("errors[1].message", is("$size operator must have integer"))
.body("errors[1].exceptionClass", is("JsonApiException"));
.body("errors[1].exceptionClass", is("JsonApiException"))
.body("errors[1].errorCode", is("INVALID_FILTER_EXPRESSION"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public void happyPath() {
error -> {
assertThat(error.message()).isEqualTo("The provided command is not implemented.");
assertThat(error.fields())
.hasSize(1)
.containsEntry("errorCode", "COMMAND_NOT_IMPLEMENTED");
.hasSize(2)
.containsEntry("errorCode", "COMMAND_NOT_IMPLEMENTED")
.containsEntry("exceptionClass", "JsonApiException");
});
}

Expand All @@ -46,8 +47,9 @@ public void withCustomMessage() {
error -> {
assertThat(error.message()).isEqualTo("Custom message is more important.");
assertThat(error.fields())
.hasSize(1)
.containsEntry("errorCode", "COMMAND_NOT_IMPLEMENTED");
.hasSize(2)
.containsEntry("errorCode", "COMMAND_NOT_IMPLEMENTED")
.containsEntry("exceptionClass", "JsonApiException");
});
}

Expand All @@ -66,8 +68,9 @@ public void withCause() {
error -> {
assertThat(error.message()).isEqualTo("The provided command is not implemented.");
assertThat(error.fields())
.hasSize(1)
.containsEntry("errorCode", "COMMAND_NOT_IMPLEMENTED");
.hasSize(2)
.containsEntry("errorCode", "COMMAND_NOT_IMPLEMENTED")
.containsEntry("exceptionClass", "JsonApiException");
})
.anySatisfy(
error -> {
Expand Down

0 comments on commit fb302f2

Please sign in to comment.