Skip to content

Simple change to support alternative error handling strategies #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pcafstockf opened this issue Jun 15, 2017 · 1 comment
Closed

Comments

@pcafstockf
Copy link

I see many requests to handle errors differently. Some simple, some quite involved.

However by simply changing the method GraphQLServlet.createResultFromDataAndErrors from private to protected access, it would be possible for subclasses to override and implement alternative error reporting/handling strategies.

My case is one of the simple ones. I would like to pass the executionId back to the client regardless of whether it is a "client error" or not. This allows me to tie an error in my browser console back to a server log.

@oliemansm
Copy link
Member

oliemansm commented Jun 17, 2017

I faced the same issue. For now I've solved it by overriding filterGraphQLErrors.

Like this:

public class GraphQLServletWithCodedErrors extends SimpleGraphQLServlet {

    public GraphQLServletWithCodedErrors(GraphQLSchemaProvider schemaProvider, ExecutionStrategyProvider executionStrategyProvider, List<GraphQLServletListener> listeners, Instrumentation instrumentation) {
        super(schemaProvider, executionStrategyProvider, listeners, instrumentation);
    }

    @Override
    protected List<GraphQLError> filterGraphQLErrors(List<GraphQLError> errors) {
        return errors.stream()
                .map(this::codedError)
                .collect(Collectors.toList());
    }

    private GraphQLError codedError(GraphQLError error) {
        if (error.getErrorType() == ErrorType.DataFetchingException) {
            ExceptionWhileDataFetching exceptionWhileDataFetching = (ExceptionWhileDataFetching) error;
            Throwable exception = exceptionWhileDataFetching.getException();
            if (exception instanceof AccessDeniedException) {
                return new CodedGraphQLError(HttpStatus.FORBIDDEN, exception.getMessage());
            } else if (exception instanceof BadCredentialsException) {
                return new CodedGraphQLError(HttpStatus.BAD_REQUEST, exception.getMessage());
            } else if (exception instanceof AuthenticationException) {
                return new CodedGraphQLError(HttpStatus.UNAUTHORIZED, exception.getMessage());
            }
        }
        return new CodedGraphQLError(HttpStatus.INTERNAL_SERVER_ERROR, error.getMessage());
    }

}

That codedError needs to be refactored a bit, but at least now I can return my own errors including status codes and any additional fields I want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants