Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit fcd48df

Browse files
committed
Make errors to specification when using default handler
fix #478
1 parent 183a563 commit fcd48df

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

graphql-kickstart-spring-support/src/main/java/graphql/kickstart/spring/error/GraphQLErrorFromExceptionHandler.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import graphql.ExceptionWhileDataFetching;
66
import graphql.GraphQLError;
77
import graphql.GraphQLException;
8+
import graphql.GraphqlErrorBuilder;
89
import graphql.SerializationError;
910
import graphql.kickstart.execution.error.DefaultGraphQLErrorHandler;
1011
import graphql.kickstart.execution.error.GenericGraphQLError;
@@ -33,10 +34,10 @@ protected List<GraphQLError> filterGraphQLErrors(List<GraphQLError> errors) {
3334

3435
private Collection<GraphQLError> transform(GraphQLError error) {
3536
ErrorContext errorContext = new ErrorContext(
36-
error.getLocations(),
37-
error.getPath(),
38-
error.getExtensions(),
39-
error.getErrorType()
37+
error.getLocations(),
38+
error.getPath(),
39+
error.getExtensions(),
40+
error.getErrorType()
4041
);
4142
return extractException(error).map(throwable -> transform(throwable, errorContext))
4243
.orElse(singletonList(new GenericGraphQLError(error.getMessage())));
@@ -60,7 +61,21 @@ private Collection<GraphQLError> transform(Throwable throwable, ErrorContext err
6061
.min(new ThrowableComparator())
6162
.map(applicables::get)
6263
.map(factory -> factory.create(throwable, errorContext))
63-
.orElse(singletonList(new ThrowableGraphQLError(throwable)));
64+
.orElseGet(() -> withThrowable(throwable, errorContext));
65+
}
66+
67+
private Collection<GraphQLError> withThrowable(Throwable throwable, ErrorContext errorContext) {
68+
Map<String, Object> extensions = Optional.ofNullable(errorContext.getExtensions()).orElseGet(HashMap::new);
69+
extensions.put("type", throwable.getClass().getSimpleName());
70+
return singletonList(
71+
GraphqlErrorBuilder.newError()
72+
.message(throwable.getMessage())
73+
.errorType(errorContext.getErrorType())
74+
.locations(errorContext.getLocations())
75+
.path(errorContext.getPath())
76+
.extensions(extensions)
77+
.build()
78+
);
6479
}
6580

6681
}

graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/spring/web/boot/GraphQLErrorHandlerTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,26 @@ public void setUp() {
4040

4141
@Test
4242
public void illegalArgumentExceptionShouldBeHandledConcretely() {
43-
TestUtils.assertGraphQLError(gql, "query { illegalArgumentException }",
44-
new ThrowableGraphQLError(new IllegalArgumentException("Illegal argument"), "Illegal argument"),
45-
objectMapper);
43+
TestUtils.assertGraphQLError(
44+
gql,
45+
"query { illegalArgumentException }",
46+
new ThrowableGraphQLError(new IllegalArgumentException("Illegal argument"), "Illegal argument"),
47+
objectMapper
48+
);
4649
}
4750

4851
@Test
4952
public void illegalStateExceptionShouldBeHandledByCatchAll() {
5053
TestUtils.assertGraphQLError(gql, "query { illegalStateException }",
51-
new ThrowableGraphQLError(new IllegalStateException("Illegal state"), "Catch all handler"),
52-
objectMapper);
54+
new ThrowableGraphQLError(new IllegalStateException("Illegal state"), "Catch all handler"),
55+
objectMapper);
5356
}
5457

5558
@Configuration
5659
static class BaseConfiguration {
5760

5861
public class Query implements GraphQLQueryResolver {
62+
5963
boolean illegalArgumentException() {
6064
throw new IllegalArgumentException("Illegal argument");
6165
}
@@ -82,9 +86,9 @@ Query queryResolver() {
8286
@Bean
8387
GraphQLSchema schema() {
8488
SchemaParser schemaParser = SchemaParser.newParser()
85-
.file("graphql/error-handler-test.graphql")
86-
.resolvers(queryResolver())
87-
.build();
89+
.file("graphql/error-handler-test.graphql")
90+
.resolvers(queryResolver())
91+
.build();
8892
return schemaParser.makeExecutableSchema();
8993
}
9094
}

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/TestUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
import java.util.HashMap;
1111
import java.util.Map;
1212
import java.util.stream.Collectors;
13+
import lombok.AccessLevel;
14+
import lombok.NoArgsConstructor;
1315
import lombok.extern.slf4j.Slf4j;
1416

1517
@Slf4j
18+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
1619
public class TestUtils {
1720

18-
private static ObjectMapper mapper = new ObjectMapper();
21+
private static final ObjectMapper mapper = new ObjectMapper();
1922

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

6568
private static String toString(GraphQLError error) {
6669
try {
67-
return mapper.writeValueAsString(error);
70+
return mapper.writeValueAsString(error.toSpecification());
6871
} catch (JsonProcessingException e) {
6972
log.error("Cannot write error {} as string", error, e);
7073
return null;

0 commit comments

Comments
 (0)