Posted By

kaqqao on 03/24/18


Tagged

graphql


Versions (?)

graphql-java-servlet error handling


 / Published in: Java
 

It worked for v 4.1 which changed the error handling logic. I haven't tried with 4.4, but it at least compiles, so I think the logic hasn't changed since

  1. // In GraphQLEndpoint:
  2. private static final GraphQLErrorHandler errorHandler = new SanitizingGraphQLErrorHandler();
  3. //...
  4. @Override
  5. protected GraphQLErrorHandler getGraphQLErrorHandler() {
  6. return errorHandler;
  7. }
  8. // The implementation
  9. public class SanitizingGraphQLErrorHandler extends DefaultGraphQLErrorHandler {
  10.  
  11. @Override
  12. protected List<GraphQLError> filterGraphQLErrors(List<GraphQLError> errors) {
  13. return errors.stream()
  14. .filter(e -> e instanceof ExceptionWhileDataFetching || super.isClientError(e))
  15. .map(e -> e instanceof ExceptionWhileDataFetching ? new SanitizedError((ExceptionWhileDataFetching) e) : e)
  16. .collect(Collectors.toList());
  17. }
  18.  
  19. public static class SanitizedError extends ExceptionWhileDataFetching {
  20.  
  21. SanitizedError(ExceptionWhileDataFetching inner) {
  22. super(inner.getException());
  23. }
  24.  
  25. @Override
  26. @JsonIgnore
  27. public Throwable getException() {
  28. return super.getException();
  29. }
  30. }
  31. }

Report this snippet  

You need to login to post a comment.