Skip to content

Commit

Permalink
closes #350: errorCode and exceptionClass to be reported in errors (#369
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Ivan Senic authored Apr 18, 2023
1 parent 9d4f4b2 commit 19b3f23
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 32 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 @@ -325,7 +325,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 @@ -348,7 +349,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 @@ -537,7 +539,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 @@ -331,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 @@ -354,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 @@ -536,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 @@ -636,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 @@ -712,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 19b3f23

Please sign in to comment.