javagraphqlreactive-programmingquarkussmallrye

Send a custom error message in smallrye-graphql error (quarkus)


I am trying to throw a custom exception in graphql but was only able to change extensions of the GraphQlError, and I want to change the error message too. Have a look,

public class SomeException extends RuntimeException implements GraphQLError {

    List<String> someErrors;
    String errorMessage;

    public SomeException(String message, List<String> someErrors) {             
        this.errorMessage = message;
        this.someErrors = someErrors;
    }

    @Override
    public String getMessage() {
        return this.errorMessage;
    }

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

    @Override
    public Map<String, Object> getExtensions() {
        return Collections.singletonMap("someErrors",someErrors);
    }

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

So, my getMessage() function actually not overridden as it still shows this...

{
    "errors": [
        {
            "message": "System error",
            "locations": [
                {
                    "line": 2,
                    "column": 3
                }
            ],
            "path": [
                "something"
            ],
            "extensions": {
                "someErrors": [
                    "...",
                    "..."
                ],
                "classification": "DataFetchingException"
            }
        }
    ],
    "data": {
        "saveInstance": null
    }
}

this is Postman response when I was triggering the exception. So, I want a custom message to be inside "message".


Solution

  • If you are using Runtime exception you need to enable it explicitly and also tell what fields you want to show. See

    https://quarkus.io/guides/smallrye-graphql#quarkus-smallrye-graphql_quarkus.smallrye-graphql.show-runtime-exception-message

    And

    https://quarkus.io/guides/smallrye-graphql#quarkus-smallrye-graphql_quarkus.smallrye-graphql.error-extension-fields