title: How to create a custom error/exception handler for graphql?
body: <p>So, I have been coding a microservice which uses GraphQL's java implementation for APIs. GraphQL enforces some level of validation for the supplied queries by client. However, in cases when the issue occurs inside resolving the query, I have seen graphql show messages which exposes the internals of the micr-service.</p>
  
  <p>What do I need? A way to handle all exceptions/errors thrown from resolver functions in such a way that I can sanitize the exceptions/errors before GraphQL creates corresponding response.</p>
  
  <p>I have looked up official documentation and many stack over flow questions, but failed to find any place it talks about handling. If I found any, they were for previous versions and are no more supported.</p>
  
  <p>Some of the links I referred - 
  1. <a href="https://www.howtographql.com/graphql-java/7-error-handling/" rel="noreferrer">https://www.howtographql.com/graphql-java/7-error-handling/</a>
  2. <a href="https://stackoverflow.com/questions/52083930/graphql-java-send-custom-error-in-json-format">GraphQL java send custom error in json format</a>
  3. <a href="https://www.graphql-java.com/documentation/v13/execution/" rel="noreferrer">https://www.graphql-java.com/documentation/v13/execution/</a></p>
  
  <p>I have already done the below things like -</p>
  
  <p>Creating custom handler </p>
  
  <pre><code>@Bean
  public GraphQLErrorHandler errorHandler() {
      return new CustomGraphQLErrorHandler();
  }
  </code></pre>
  
  <pre><code>public class CustomGraphQLErrorHandler implements GraphQLErrorHandler {
  
      @Override
      public List&lt;GraphQLError&gt; processErrors(List&lt;GraphQLError&gt; errors) {
          List&lt;GraphQLError&gt; clientErrors = errors.stream()
                  .filter(this::isClientError)
                  .collect(Collectors.toList());
  
          List&lt;GraphQLError&gt; serverErrors = errors.stream()
                  .filter(this::isSystemError)
                  .map(GraphQLErrorAdapter::new)
                  .collect(Collectors.toList());
  
          List&lt;GraphQLError&gt; e = new ArrayList&lt;&gt;();
          e.addAll(clientErrors);
          e.addAll(serverErrors);
          return e;
      }
  
      private boolean isSystemError(GraphQLError error) {
          return !isClientError(error);
      }
  
      private boolean isClientError(GraphQLError error) {
          return !(error instanceof ExceptionWhileDataFetching || error instanceof Throwable);
      }
  }```
  
  Expected behavior - The control would reach to `processErrors` method. Actual - It doesn't reach there.
  </code></pre>