Skip to content

Expose response body and GraphQL errors on GraphQLHttpException - #16

Merged
clement911 merged 2 commits into
mainfrom
feat/http-error-response-details
Nov 5, 2026
Merged

clement911 merged 2 commits into
mainfrom
feat/http-error-response-details

Conversation

@clement911

@clement911 clement911 commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Alternative to #15, with a design based on the existing GraphQL types and the GraphQL over HTTP spec.

What

When a request fails with a non-success HTTP status, GraphQLHttpException now exposes:

  • ResponseBody (string): the raw response body, or null if it was empty or could not be read.
  • Response (GraphQLResponse): set when the body is a GraphQL response, otherwise null. It is parsed only when the content type is application/json or application/graphql-response+json and the body has errors or data. It reuses the existing GraphQLError model, so extensions (e.g. code), path and locations are kept.

The exception message adds the GraphQL errors when they were parsed. Otherwise it adds the raw body, truncated to 1000 characters.

Design notes

  • The HTTP error is always preserved. Reading and parsing the body is best effort. Any failure, including I/O errors, cancellation and invalid JSON, is swallowed, so the client still throws GraphQLHttpException and RetryInterceptor behaves as before.
  • Follows the GraphQL over HTTP spec. A non-2xx body may come from an intermediary such as a proxy or gateway, so non-JSON bodies are never parsed. JSON that doesn't look like a GraphQL response stays only in ResponseBody.
  • Only spec-compliant error responses are parsed. errors must be an array of error objects. Any other shape, such as a string errors, leaves Response null and is only available through ResponseBody and the message.
  • No breaking change. The existing constructor stays, and a new overload takes the body and the parsed response.

Tests

GraphQLHttpExceptionTests covers:

  • a spec-compliant 400 response with extensions
  • a non-array errors
  • non-GraphQL JSON
  • an HTML 502 with truncation in the message
  • an empty body
  • retries still happening when the error body is a GraphQL error

🤖 Generated with Claude Code

clement911 and others added 2 commits November 6, 2026 09:10
On a non-success HTTP status, read the response body (best effort) and
expose it on GraphQLHttpException:
- ResponseBody: the raw body, always available when non-empty
- Response: a GraphQLResponse parsed from the body when it is JSON and
  looks like a GraphQL response (GraphQL over HTTP spec), reusing the
  existing GraphQLError model (message, locations, path, extensions)

Failures while reading or parsing the body never replace the HTTP error,
so retries and exception types are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@clement911
clement911 merged commit 2229736 into main Nov 5, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant