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

Commit 6ff7138

Browse files
authored
Merge pull request #454 from tfij/AddSupportForLocationAndPathInErrorHandler
Add suport for location and path to error handler
2 parents da01402 + e27bda3 commit 6ff7138

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package graphql.kickstart.spring.error;
2+
3+
import graphql.language.SourceLocation;
4+
5+
import java.util.List;
6+
7+
public class ErrorContext {
8+
private final List<SourceLocation> locations;
9+
private final List<Object> path;
10+
11+
public ErrorContext(List<SourceLocation> locations, List<Object> path) {
12+
this.locations = locations;
13+
this.path = path;
14+
}
15+
16+
public List<SourceLocation> getLocations() {
17+
return locations;
18+
}
19+
20+
public List<Object> getPath() {
21+
return path;
22+
}
23+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface GraphQLErrorFactory {
1010

1111
Optional<Class<? extends Throwable>> mostConcrete(Throwable t);
1212

13-
Collection<GraphQLError> create(Throwable t);
13+
Collection<GraphQLError> create(Throwable t, ErrorContext errorContext);
1414

1515
static GraphQLErrorFactory withReflection(Object object, Method method) {
1616
return new ReflectiveGraphQLErrorFactory(object, method);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ protected List<GraphQLError> filterGraphQLErrors(List<GraphQLError> errors) {
3232
}
3333

3434
private Collection<GraphQLError> transform(GraphQLError error) {
35-
return extractException(error).map(this::transform)
35+
ErrorContext errorContext = new ErrorContext(error.getLocations(), error.getPath());
36+
return extractException(error).map(throwable -> transform(throwable, errorContext))
3637
.orElse(singletonList(new GenericGraphQLError(error.getMessage())));
3738
}
3839

@@ -47,13 +48,13 @@ private Optional<Throwable> extractException(GraphQLError error) {
4748
return Optional.empty();
4849
}
4950

50-
private Collection<GraphQLError> transform(Throwable throwable) {
51+
private Collection<GraphQLError> transform(Throwable throwable, ErrorContext errorContext) {
5152
Map<Class<? extends Throwable>, GraphQLErrorFactory> applicables = new HashMap<>();
5253
factories.forEach(factory -> factory.mostConcrete(throwable).ifPresent(t -> applicables.put(t, factory)));
5354
return applicables.keySet().stream()
5455
.min(new ThrowableComparator())
5556
.map(applicables::get)
56-
.map(factory -> factory.create(throwable))
57+
.map(factory -> factory.create(throwable, errorContext))
5758
.orElse(singletonList(new ThrowableGraphQLError(throwable)));
5859
}
5960

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class ReflectiveGraphQLErrorFactory implements GraphQLErrorFactory {
1616

1717
private final boolean singularReturnType;
18+
private final boolean withErrorContext;
1819
private Object object;
1920
private Method method;
2021
private Throwables throwables;
@@ -23,7 +24,7 @@ class ReflectiveGraphQLErrorFactory implements GraphQLErrorFactory {
2324
this.object = object;
2425
this.method = method;
2526
singularReturnType = GraphQLError.class.isAssignableFrom(method.getReturnType());
26-
27+
withErrorContext = method.getParameterCount() == 2;
2728
throwables = new Throwables(method.getAnnotation(ExceptionHandler.class).value());
2829
}
2930

@@ -33,17 +34,25 @@ public Optional<Class<? extends Throwable>> mostConcrete(Throwable t) {
3334
}
3435

3536
@Override
36-
public Collection<GraphQLError> create(Throwable t) {
37+
public Collection<GraphQLError> create(Throwable t, ErrorContext errorContext) {
3738
try {
3839
method.setAccessible(true);
3940
if (singularReturnType) {
40-
return singletonList((GraphQLError) method.invoke(object, t));
41+
return singletonList((GraphQLError) invoke(t, errorContext));
4142
}
42-
return (Collection<GraphQLError>) method.invoke(object, t);
43+
return (Collection<GraphQLError>) invoke(t, errorContext);
4344
} catch (IllegalAccessException | InvocationTargetException e) {
4445
log.error("Cannot create GraphQLError from throwable {}", t.getClass().getSimpleName(), e);
4546
return singletonList(new GenericGraphQLError(t.getMessage()));
4647
}
4748
}
4849

50+
private Object invoke(Throwable t, ErrorContext errorContext) throws IllegalAccessException, InvocationTargetException {
51+
if (withErrorContext) {
52+
return method.invoke(object, t, errorContext);
53+
} else {
54+
return method.invoke(object, t);
55+
}
56+
}
57+
4958
}

0 commit comments

Comments
 (0)