question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot override SimpleDataFetcherExceptionHandler

See original GitHub issue

Hi, Thank you for this great implementation. I am using version 7.0.1. Facing problem with exception handling. Tried lots of different ways. like this one: https://github.com/leangen/graphql-spqr/issues/219

`@Configuration public class GraphQLConfig {

@Bean public GraphQL graphQL(GraphQLSchema schema) { return GraphQL.newGraphQL(schema) .queryExecutionStrategy(new AsyncExecutionStrategy(new CustomExceptionHandler())) .mutationExecutionStrategy(new AsyncSerialExecutionStrategy(new CustomExceptionHandler())) .build(); } }Also have public class CustomExceptionHandler implements DataFetcherExceptionHandler {

@Override public DataFetcherExceptionHandlerResult onException( DataFetcherExceptionHandlerParameters handlerParameters) {

Throwable exception = handlerParameters.getException();

// do something with exception
System.out.println("asdadad");

GraphQLError error = GraphqlErrorBuilder.newError().message(exception.getMessage()).build();

return DataFetcherExceptionHandlerResult.newResult().error(error).build();

} }`

My CustomExceptionHandler is not triggering. Please, help to solve this issue. P.S: I am new in GraphQL, also sorry my english.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
erdenebadralcommented, Apr 8, 2020

Thank you very much. If someone looking code example:

public class CustomGraphQLError implements GraphQLError {

  private static final long serialVersionUID = 1L;

  private String message;
  private final Map<String, Object> extensions;

  public CustomGraphQLError(String message, Map<String, Object> extensions) {
    this.extensions = extensions;
    this.message = message;
  }
  @Override
  public String getMessage() {
    return message;
  }

  @Override
  public List<SourceLocation> getLocations() {
    return null;
  }

  @Override
  public ErrorClassification getErrorType() {
    return null;
  }

  @Override
  public Map<String, Object> getExtensions() {
    return extensions;
  }

}
@Component
public class GraphQLExceptionHandler {

  @ExceptionHandler(CustomValidationException.class)
  GraphQLError myCustomHandler(CustomValidationException e) {
    CustomGraphQLError customError = new CustomGraphQLError(e.getMessage(), e.getExtensions());
    return customError;
  }

}
public class CustomValidationException extends RuntimeException implements GraphQLError {

  private static final long serialVersionUID = 1L;
  private final HashMap<String, Object> extensions;

  public CustomValidationException(Map<String, String> message) {
    super("Validation Error");
    extensions = new HashMap<String, Object>();
    extensions.putAll(message);
  }

  @Override
  public List<SourceLocation> getLocations() {    
    return null;
  }

  @Override
  public ErrorType getErrorType() {
    return null;
  }

  @Override
  public Map<String, Object> getExtensions() {
    return extensions;
  }

}
@Transactional
  public Book addBook(BookInput input) {
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();

    Book book =
        Book.builder().name(input.getName()).publishedDate(input.getPublishedDate()).build();
    Set<ConstraintViolation<Book>> constraintViolations = validator.validate(book);
    if (constraintViolations.isEmpty()) {
      return bookRepository.save(book);
    } else {
      Map<String, String> message = new HashMap<String, String>();
      for (ConstraintViolation<Book> contraints : constraintViolations) {
        message.put(contraints.getPropertyPath().toString(), contraints.getMessage());
      }
      throw new CustomValidationException(message);
    }

  }
0reactions
oliemansmcommented, Apr 7, 2020

You could create a custom implementation of GraphQLError and return that. If you return null for the locations and just limit the values returned by getExtensions() you should get the result you’re after.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to override DataFetcherExceptionHandler in GraphQL ...
I am not sure how to override the SimpleDataFetcherExceptionHandler with my own CustomExceptionHandler. The library already autowires a lot of ...
Read more >
Understanding GraphQL Error Handling ... - DEV Community ‍ ‍
Overriding getLocations() and getErrorType() is mandatory. We will just return null in these methods because they are ignored by the default ...
Read more >
Execution - GraphQL Java
getLogger(SimpleDataFetcherExceptionHandler.class); @Override public void accept(DataFetcherExceptionHandlerParameters handlerParameters) {
Read more >
Understanding GraphQL Error Handling ... - Medium
Overriding getLocations() and getErrorType() is mandatory. We will just return null in these methods because they are ignored by the default ...
Read more >
leangen/graphql-spqr - Gitter
When setting up the GraphQL bean, I can provide a custom exception handler (not error handler) and thereby override SimpleDataFetcherExceptionHandler
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found