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

closes #350: errorCode and exceptionClass to be reported always with … #369

Merged
merged 1 commit into from
Apr 18, 2023
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 @@ -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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the JsonApiException was not including the exceptionClass before so I added it, I guess this is also helpful information..

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);
Copy link
Contributor Author

@ivansenic ivansenic Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I adapted this, the ExceptionUtil should not know how are CommandResult.Error instances created, cause we have a single place for that.. Adapted for custom 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