Skip to content

Commit 3001c7a

Browse files
karl-runapottere
authored andcommitted
Check if inner exception is of type GraphQLError in DefaultGraphQLErrorHandler (#51)
1 parent 71d1b26 commit 3001c7a

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ protected List<GraphQLError> filterGraphQLErrors(List<GraphQLError> errors) {
4444
}
4545

4646
protected boolean isClientError(GraphQLError error) {
47-
return !(error instanceof ExceptionWhileDataFetching || error instanceof Throwable);
47+
if (error instanceof ExceptionWhileDataFetching) {
48+
return ((ExceptionWhileDataFetching) error).getException() instanceof GraphQLError;
49+
}
50+
return !(error instanceof Throwable);
4851
}
4952
}

src/test/groovy/graphql/servlet/GraphQLServletSpec.groovy

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,23 @@ class GraphQLServletSpec extends Specification {
641641
errors.first().message.startsWith("Internal Server Error(s)")
642642
}
643643

644+
def "errors that also implement GraphQLError thrown while data fetching are passed to caller"() {
645+
setup:
646+
servlet = createServlet({ throw new TestGraphQLErrorException("This is a test message") })
647+
request.addParameter('query', 'query { echo(arg:"test") }')
648+
649+
when:
650+
servlet.doGet(request, response)
651+
652+
then:
653+
response.getStatus() == STATUS_OK
654+
response.getContentType() == CONTENT_TYPE_JSON_UTF8
655+
def errors = getResponseContent().errors
656+
errors.size() == 1
657+
errors.first().extensions.foo == "bar"
658+
errors.first().message.startsWith("Exception while fetching data (/echo) : This is a test message")
659+
}
660+
644661
def "batched errors while data fetching are masked in the response"() {
645662
setup:
646663
servlet = createServlet({ throw new TestException() })
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package graphql.servlet
2+
3+
import graphql.ErrorType
4+
import graphql.GraphQLError
5+
import graphql.language.SourceLocation
6+
7+
/**
8+
* @author Andrew Potter
9+
*/
10+
class TestGraphQLErrorException extends RuntimeException implements GraphQLError {
11+
12+
public TestGraphQLErrorException(String message) {
13+
super(message);
14+
}
15+
16+
@Override
17+
public Map<String, Object> getExtensions() {
18+
Map<String, Object> customAttributes = new LinkedHashMap<>();
19+
customAttributes.put("foo", "bar");
20+
return customAttributes;
21+
}
22+
23+
@Override
24+
List<SourceLocation> getLocations() {
25+
return null
26+
}
27+
28+
@Override
29+
ErrorType getErrorType() {
30+
return ErrorType.ValidationError;
31+
}
32+
}

0 commit comments

Comments
 (0)