SonarSource/sonarqube · error · IllegalStateException

The GraphQl answer contains errors:

Error message

The GraphQl answer contains errors: 

What it means

If the payload deserializes but the GsonGraphQlAnswer carries a non-empty `errors` array (isValid() false), the client throws an IllegalStateException listing the GraphQL errors. This means the HTTP transport succeeded but the server rejected the query itself — bad field names, missing arguments, permission issues on the queried nodes.

Solutions

  1. Read the `errors` array from the exception message to see the exact server complaints.
  2. Update the query to match the current server schema (use introspection or GraphiQL).
  3. Check that the authenticated user/token has permission for the requested resources.
  4. Validate query arguments (ids, filters) actually exist and are correctly formatted.
  5. Upgrade the client/API types alongside the server to avoid schema drift.

Example fix

// before
query { projects { data { id name removedField } } }
// after
query { projects { data { id name } } } // removedField no longer in schema
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate the query against the current schema before shipping:
// run it in GraphiQL / introspection diff in CI

Try / catch

try {
  return graphQlClient.fetchValidDataOrThrow(type, request);
} catch (IllegalStateException e) {
  if (e.getMessage().startsWith("The GraphQl answer contains errors")) {
    log.warn("GraphQL query rejected: {}", e.getMessage());
    throw new QueryValidationException(e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Executing a GraphQL query whose response includes a top-level `errors` array: unknown fields after a server schema change, insufficient permissions on requested entities, invalid arguments, or a partially failing query.

Common situations: Client not upgraded after a server upgrade (schema drift); querying components/projects the token's user cannot see; hard-coded queries referencing removed fields; nullability issues surfacing as errors.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/c44aa4ae9ca80cf0. Report an issue: GitHub.

Appendix: source

Thrown at server/sonar-server-common/src/main/java/org/sonar/server/common/graphql/GraphQlClient.java:157

  private static <T> GsonGraphQlAnswer<T> deserializeValidAnswerOrThrow(Type answerDataType, Response response) {
    GsonGraphQlAnswer<T> graphQlAnswer;
    Function<String, GsonGraphQlAnswer<T>> toObject = stringPayload -> GSON.fromJson(stringPayload, answerDataType);
    Optional<String> bodyString = Optional.empty();
    try {
      bodyString = Optional.ofNullable(response.body()).map(body -> {
        try {
          return body.string();
        } catch (IOException e) {
          throw new IllegalStateException(e);
        }
      });
      graphQlAnswer = bodyString.map(toObject).orElseThrow();
    } catch (Exception exception) {
      throw new IllegalStateException(format("Error while deserializing GraphQL payload: %s. %s", exception.getMessage(), bodyString), exception);
    }
    if (!graphQlAnswer.isValid()) {
      throw new IllegalStateException("The GraphQl answer contains errors: " + graphQlAnswer.errors());
    }
    return graphQlAnswer;
  }

}

View on GitHub (pinned to 184c821202)