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

Commit 3ae07c5

Browse files
committed
Only call GraphqlErrorBuilder with non-null data
The methods on GraphqlErrorBuilder each contain [an assertion that the supplied data is non-null](https://github.com/graphql-java/graphql-java/blob/master/src/main/java/graphql/GraphqlErrorBuilder.java#L77), but it is not required that these elements be non-null on every throw GraphQLError. For example, errors often omit a `path` property, which will lead to an unhandled `graphql.AssertException` when the error is handled by `GraphQLErrorFromExceptionHandler.java`.
1 parent 9dc6d2e commit 3ae07c5

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,13 @@ private Collection<GraphQLError> withThrowable(Throwable throwable, ErrorContext
7171
Map<String, Object> extensions = Optional.ofNullable(errorContext.getExtensions())
7272
.orElseGet(HashMap::new);
7373
extensions.put("type", throwable.getClass().getSimpleName());
74-
return singletonList(
75-
GraphqlErrorBuilder.newError()
76-
.message(throwable.getMessage())
77-
.errorType(errorContext.getErrorType())
78-
.locations(errorContext.getLocations())
79-
.path(errorContext.getPath())
80-
.extensions(extensions)
81-
.build()
82-
);
74+
GraphqlErrorBuilder builder = GraphqlErrorBuilder.newError()
75+
.extensions(extensions);
76+
Optional.ofNullable(throwable.getMessage()).ifPresent(builder::message);
77+
Optional.ofNullable(errorContext.getErrorType()).ifPresent(builder::errorType);
78+
Optional.ofNullable(errorContext.getLocations()).ifPresent(builder::locations);
79+
Optional.ofNullable(errorContext.getPath()).ifPresent(builder::path);
80+
return singletonList(builder.build());
8381
}
8482

8583
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package graphql.kickstart.spring.error;
2+
3+
import graphql.ErrorType;
4+
import graphql.GraphQLError;
5+
import graphql.GraphqlErrorException;
6+
import graphql.language.SourceLocation;
7+
import java.util.List;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
class GraphQLErrorFromExceptionHandlerTest {
13+
@Test
14+
void allows_errors_with_null_path() {
15+
GraphQLErrorFromExceptionHandler sut = new GraphQLErrorFromExceptionHandler(List.of());
16+
17+
List<GraphQLError> errors = List.of(
18+
GraphqlErrorException.newErrorException()
19+
.message("Error without a path")
20+
.sourceLocation(new SourceLocation(0, 0))
21+
.build(),
22+
GraphqlErrorException.newErrorException()
23+
.message("Error with path")
24+
.sourceLocation(new SourceLocation(0, 0))
25+
.errorClassification(ErrorType.ValidationError)
26+
.path(List.of())
27+
.build());
28+
List<GraphQLError> processedErrors = sut.filterGraphQLErrors(errors);
29+
30+
for (int i = 0; i < errors.size(); i++) {
31+
GraphQLError error = errors.get(i);
32+
GraphQLError processedError = processedErrors.get(i);
33+
assertEquals(error.getMessage(), processedError.getMessage());
34+
assertEquals(error.getErrorType(), processedError.getErrorType());
35+
assertEquals(error.getLocations(), processedError.getLocations());
36+
assertEquals(error.getPath(), processedError.getPath());
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)