Skip to content

Commit aaa1b49

Browse files
committed
Added proposed decorator for fixing rendering issue (fix #66)
1 parent 19fd060 commit aaa1b49

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

src/main/java/graphql/servlet/DefaultGraphQLErrorHandler.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import graphql.ExceptionWhileDataFetching;
44
import graphql.GraphQLError;
5+
import graphql.execution.NonNullableFieldWasNullError;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78

@@ -24,25 +25,26 @@ public List<GraphQLError> processErrors(List<GraphQLError> errors) {
2425
clientErrors.add(new GenericGraphQLError("Internal Server Error(s) while executing query"));
2526

2627
errors.stream()
27-
.filter(error -> !isClientError(error))
28-
.forEach(error -> {
29-
if(error instanceof Throwable) {
30-
log.error("Error executing query!", (Throwable) error);
31-
} else if (error instanceof ExceptionWhileDataFetching) {
32-
log.error("Error executing query {}", error.getMessage(), ((ExceptionWhileDataFetching) error).getException());
33-
} else {
34-
log.error("Error executing query ({}): {}", error.getClass().getSimpleName(), error.getMessage());
35-
}
36-
});
28+
.filter(error -> !isClientError(error))
29+
.forEach(error -> {
30+
if (error instanceof Throwable) {
31+
log.error("Error executing query!", (Throwable) error);
32+
} else if (error instanceof ExceptionWhileDataFetching) {
33+
log.error("Error executing query {}", error.getMessage(), ((ExceptionWhileDataFetching) error).getException());
34+
} else {
35+
log.error("Error executing query ({}): {}", error.getClass().getSimpleName(), error.getMessage());
36+
}
37+
});
3738
}
3839

3940
return clientErrors;
4041
}
4142

4243
protected List<GraphQLError> filterGraphQLErrors(List<GraphQLError> errors) {
4344
return errors.stream()
44-
.filter(this::isClientError)
45-
.collect(Collectors.toList());
45+
.filter(this::isClientError)
46+
.map(this::replaceNonNullableFieldWasNullError)
47+
.collect(Collectors.toList());
4648
}
4749

4850
protected boolean isClientError(GraphQLError error) {
@@ -51,4 +53,12 @@ protected boolean isClientError(GraphQLError error) {
5153
}
5254
return !(error instanceof Throwable);
5355
}
56+
57+
private GraphQLError replaceNonNullableFieldWasNullError(GraphQLError error) {
58+
if (error instanceof NonNullableFieldWasNullError) {
59+
return new RenderableNonNullableFieldWasNullError((NonNullableFieldWasNullError) error);
60+
} else {
61+
return error;
62+
}
63+
}
5464
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package graphql.servlet;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import graphql.ErrorType;
5+
import graphql.GraphQLError;
6+
import graphql.execution.NonNullableFieldWasNullError;
7+
import graphql.language.SourceLocation;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
class RenderableNonNullableFieldWasNullError implements GraphQLError {
13+
14+
private final NonNullableFieldWasNullError delegate;
15+
16+
public RenderableNonNullableFieldWasNullError(NonNullableFieldWasNullError nonNullableFieldWasNullError) {
17+
this.delegate = nonNullableFieldWasNullError;
18+
}
19+
20+
@Override
21+
public String getMessage() {
22+
return delegate.getMessage();
23+
}
24+
25+
@Override
26+
@JsonInclude(JsonInclude.Include.NON_NULL)
27+
public List<SourceLocation> getLocations() {
28+
return delegate.getLocations();
29+
}
30+
31+
@Override
32+
public ErrorType getErrorType() {
33+
return delegate.getErrorType();
34+
}
35+
36+
@Override
37+
public List<Object> getPath() {
38+
return delegate.getPath();
39+
}
40+
41+
@Override
42+
public Map<String, Object> toSpecification() {
43+
return delegate.toSpecification();
44+
}
45+
46+
@Override
47+
@JsonInclude(JsonInclude.Include.NON_NULL)
48+
public Map<String, Object> getExtensions() {
49+
return delegate.getExtensions();
50+
}
51+
52+
}

0 commit comments

Comments
 (0)