Skip to content

Commit

Permalink
Make errors to specification when using default handler
Browse files Browse the repository at this point in the history
  • Loading branch information
oliemansm committed Nov 13, 2020
1 parent 183a563 commit fcd48df
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import graphql.ExceptionWhileDataFetching;
import graphql.GraphQLError;
import graphql.GraphQLException;
import graphql.GraphqlErrorBuilder;
import graphql.SerializationError;
import graphql.kickstart.execution.error.DefaultGraphQLErrorHandler;
import graphql.kickstart.execution.error.GenericGraphQLError;
Expand Down Expand Up @@ -33,10 +34,10 @@ protected List<GraphQLError> filterGraphQLErrors(List<GraphQLError> errors) {

private Collection<GraphQLError> transform(GraphQLError error) {
ErrorContext errorContext = new ErrorContext(
error.getLocations(),
error.getPath(),
error.getExtensions(),
error.getErrorType()
error.getLocations(),
error.getPath(),
error.getExtensions(),
error.getErrorType()
);
return extractException(error).map(throwable -> transform(throwable, errorContext))
.orElse(singletonList(new GenericGraphQLError(error.getMessage())));
Expand All @@ -60,7 +61,21 @@ private Collection<GraphQLError> transform(Throwable throwable, ErrorContext err
.min(new ThrowableComparator())
.map(applicables::get)
.map(factory -> factory.create(throwable, errorContext))
.orElse(singletonList(new ThrowableGraphQLError(throwable)));
.orElseGet(() -> withThrowable(throwable, errorContext));
}

private Collection<GraphQLError> withThrowable(Throwable throwable, ErrorContext errorContext) {
Map<String, Object> extensions = Optional.ofNullable(errorContext.getExtensions()).orElseGet(HashMap::new);
extensions.put("type", throwable.getClass().getSimpleName());
return singletonList(
GraphqlErrorBuilder.newError()
.message(throwable.getMessage())
.errorType(errorContext.getErrorType())
.locations(errorContext.getLocations())
.path(errorContext.getPath())
.extensions(extensions)
.build()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,26 @@ public void setUp() {

@Test
public void illegalArgumentExceptionShouldBeHandledConcretely() {
TestUtils.assertGraphQLError(gql, "query { illegalArgumentException }",
new ThrowableGraphQLError(new IllegalArgumentException("Illegal argument"), "Illegal argument"),
objectMapper);
TestUtils.assertGraphQLError(
gql,
"query { illegalArgumentException }",
new ThrowableGraphQLError(new IllegalArgumentException("Illegal argument"), "Illegal argument"),
objectMapper
);
}

@Test
public void illegalStateExceptionShouldBeHandledByCatchAll() {
TestUtils.assertGraphQLError(gql, "query { illegalStateException }",
new ThrowableGraphQLError(new IllegalStateException("Illegal state"), "Catch all handler"),
objectMapper);
new ThrowableGraphQLError(new IllegalStateException("Illegal state"), "Catch all handler"),
objectMapper);
}

@Configuration
static class BaseConfiguration {

public class Query implements GraphQLQueryResolver {

boolean illegalArgumentException() {
throw new IllegalArgumentException("Illegal argument");
}
Expand All @@ -82,9 +86,9 @@ Query queryResolver() {
@Bean
GraphQLSchema schema() {
SchemaParser schemaParser = SchemaParser.newParser()
.file("graphql/error-handler-test.graphql")
.resolvers(queryResolver())
.build();
.file("graphql/error-handler-test.graphql")
.resolvers(queryResolver())
.build();
return schemaParser.makeExecutableSchema();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TestUtils {

private static ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper mapper = new ObjectMapper();

public static Map<String, Object> assertNoGraphQLErrors(GraphQL gql, String query) {
return assertNoGraphQLErrors(gql, new HashMap<>(), new Object(), query);
Expand Down Expand Up @@ -64,7 +67,7 @@ public static void assertGraphQLError(GraphQL gql, String query, GraphQLError er

private static String toString(GraphQLError error) {
try {
return mapper.writeValueAsString(error);
return mapper.writeValueAsString(error.toSpecification());
} catch (JsonProcessingException e) {
log.error("Cannot write error {} as string", error, e);
return null;
Expand Down

0 comments on commit fcd48df

Please sign in to comment.