Custom error handling not working between Spring Boot GraphQL backend and frontend

I’m trying to send custom error details from my Spring Boot GraphQL backend to the frontend but it’s not working properly.

When I create a mutation method like this:

@MutationMapping
public Mono<User> testError() {
    return Mono.error(new CustomErrorException("Test error", "Details for frontend"));
}

The frontend receives a generic Apollo error instead of my custom error details:

{
    "errors": [
        {
            "message": "INTERNAL_ERROR for abc123-45",
            "locations": [
                {
                    "line": 1,
                    "column": 2
                }
            ],
            "path": [
                "testError"
            ],
            "extensions": {
                "classification": "INTERNAL_ERROR"
            }
        }
    ],
    "data": {
        "testError": null
    }
}

I also tried extending GraphQLError but still can’t get the custom error info to reach the frontend. What’s the right way to handle this?

Interesting issue! Are u running this in dev or production? Spring Boot GraphQL acts differently based on the environment. Also, what’s ur CustomErrorException class look like? Does it implement getExtensions() properly?

Spring Boot GraphQL sanitizes error messages by default in production, which is why you’re not seeing your custom errors. You’ll need to create a custom DataFetcherExceptionResolverAdapter to fix this. Make a config class that extends the adapter and override resolveToSingleError to return your CustomErrorException details in the extensions field. Also, make sure your CustomErrorException implements GraphQLError with a proper getExtensions method that returns a map of your custom error data. This way you keep things secure but still send the error info your frontend needs.

u might be right about Spring Boot’s error filtering. have you tried using the DataFetcherExceptionResolver? it can help u manage error responses better, so ur custom details get through instead of just that generic error.