Skip to content

Commit 7bd01a5

Browse files
authored
Merge pull request graphql-java#387 from corydolphin/exception-handling-strategy
Allow ExecutionStrategy to specify dataFetchingExceptionHandler
2 parents cb7917d + 0f0a15f commit 7bd01a5

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

src/main/java/graphql/execution/ExecutionStrategy.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ public abstract class ExecutionStrategy {
4343

4444
public abstract ExecutionResult execute(ExecutionContext executionContext, ExecutionParameters parameters) throws NonNullableFieldWasNullException;
4545

46+
/**
47+
* Handle exceptions which occur during data fetching. By default, add all exceptions to the execution context's
48+
* error's. Subclasses may specify custom handling, e.g. of different behavior with different exception types (e.g.
49+
* re-throwing certain exceptions).
50+
* @param executionContext
51+
* @param fieldDef
52+
* @param argumentValues
53+
* @param e
54+
*/
55+
protected void handleDataFetchingException(
56+
ExecutionContext executionContext,
57+
GraphQLFieldDefinition fieldDef,
58+
Map<String, Object> argumentValues,
59+
Exception e) {
60+
executionContext.addError(new ExceptionWhileDataFetching(e));
61+
}
62+
63+
4664
protected ExecutionResult resolveField(ExecutionContext executionContext, ExecutionParameters parameters, List<Field> fields) {
4765
GraphQLObjectType type = parameters.typeInfo().castType(GraphQLObjectType.class);
4866
GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), type, fields.get(0));
@@ -70,8 +88,7 @@ protected ExecutionResult resolveField(ExecutionContext executionContext, Execut
7088
fetchCtx.onEnd(resolvedValue);
7189
} catch (Exception e) {
7290
log.warn("Exception while fetching data", e);
73-
executionContext.addError(new ExceptionWhileDataFetching(e));
74-
91+
handleDataFetchingException(executionContext, fieldDef, argumentValues, e);
7592
fetchCtx.onEnd(e);
7693
}
7794

src/main/java/graphql/execution/batched/BatchedExecutionStrategy.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package graphql.execution.batched;
22

3-
import graphql.ExceptionWhileDataFetching;
43
import graphql.ExecutionResult;
54
import graphql.ExecutionResultImpl;
65
import graphql.GraphQLException;
@@ -267,12 +266,9 @@ private List<GraphQLExecutionNodeValue> fetchData(ExecutionContext executionCont
267266
try {
268267
values = (List<Object>) getDataFetcher(fieldDef).get(environment);
269268
} catch (Exception e) {
270-
values = new ArrayList<>();
271-
for (int i = 0; i < nodeData.size(); i++) {
272-
values.add(null);
273-
}
269+
values = new ArrayList<>(nodeData.size());
274270
log.warn("Exception while fetching data", e);
275-
executionContext.addError(new ExceptionWhileDataFetching(e));
271+
handleDataFetchingException(executionContext, fieldDef, argumentValues, e);
276272
}
277273
assert nodeData.size() == values.size();
278274

0 commit comments

Comments
 (0)