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

Commit e9b96f9

Browse files
authored
Merge pull request #568 from jeskew-gov/allow-errors-with-null-paths
Only call GraphqlErrorBuilder with non-null data
2 parents f8b17d9 + 140d935 commit e9b96f9

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-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,41 @@
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.ArrayList;
8+
import java.util.List;
9+
import org.junit.jupiter.api.Test;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
class GraphQLErrorFromExceptionHandlerTest {
14+
@Test
15+
void allows_errors_with_null_path() {
16+
GraphQLErrorFromExceptionHandler sut = new GraphQLErrorFromExceptionHandler(new ArrayList<>());
17+
18+
List<GraphQLError> errors = new ArrayList<>();
19+
errors.add(GraphqlErrorException.newErrorException()
20+
.message("Error without a path")
21+
.sourceLocation(new SourceLocation(0, 0))
22+
.build());
23+
errors.add(GraphqlErrorException.newErrorException()
24+
.message("Error with path")
25+
.sourceLocation(new SourceLocation(0, 0))
26+
.errorClassification(ErrorType.ValidationError)
27+
.path(new ArrayList<>())
28+
.build());
29+
30+
List<GraphQLError> processedErrors = sut.filterGraphQLErrors(errors);
31+
32+
for (int i = 0; i < errors.size(); i++) {
33+
GraphQLError error = errors.get(i);
34+
GraphQLError processedError = processedErrors.get(i);
35+
assertEquals(error.getMessage(), processedError.getMessage());
36+
assertEquals(error.getErrorType(), processedError.getErrorType());
37+
assertEquals(error.getLocations(), processedError.getLocations());
38+
assertEquals(error.getPath(), processedError.getPath());
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)