Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 28, 2026

EnsureSuccessStatusCode throws without the response body, making server error diagnostics difficult when the body contains error details (e.g., 400 Bad Request with validation messages).

Changes

  • New HttpResponseMessageExtensions in src/Common/:

    • EnsureSuccessStatusCodeWithResponseBodyAsync() - reads body before throwing (with 5-second timeout to prevent hangs)
    • CreateHttpRequestException() - constructs exception with body in message
    • Catches all exceptions when reading response body (including cancellation) to ensure the HTTP error is always thrown
    • Truncates large response bodies to 1KB for readability
  • Updated transport clients to use the new method:

    • SseClientSessionTransport (connect + send)
    • StreamableHttpClientSessionTransport (send)
    • ClientOAuthProvider (token exchange + metadata fetch)

Result

Before:

HttpRequestException: Response status code does not indicate success: 400 (Bad Request).

After:

HttpRequestException: Response status code does not indicate success: 400 (Bad Request). Response body: {"error":"invalid_grant","error_description":"Authorization code expired"}

HttpRequestException.StatusCode is preserved on .NET targets.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits January 28, 2026 23:10
…ents

Replace EnsureSuccessStatusCode with new extension method that reads
response body and includes it in the exception message to help diagnose
server errors. The HttpRequestException type and StatusCode are preserved.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Address code review feedback to not swallow cancellation exceptions
when reading the response body.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Update EnsureSuccessStatusCode usage for better error diagnosis Include response body in HttpRequestException for transport client errors Jan 28, 2026
Copilot AI requested a review from stephentoub January 28, 2026 23:19
Prevents hanging in edge cases where server sends error response but
doesn't end the request. Linked CancellationTokenSource ensures the
original cancellation token is still honored.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@stephentoub stephentoub marked this pull request as ready for review January 29, 2026 01:18
@stephentoub stephentoub requested a review from halter73 January 29, 2026 01:19
Catch all exceptions including cancellation when reading error response
body, since the important thing is throwing the HttpRequestException
with the status code. Getting the body is just a "nice to have".

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
halter73
halter73 previously approved these changes Jan 29, 2026
Copy link
Contributor

@halter73 halter73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we trim the body if it's too large for readability in the normal case where the error body isn't that interesting and just represents an outage or something like that?

@stephentoub
Copy link
Contributor

@copilot, please address @halter73's comments

- Remove comment about timeout as suggested by @halter73
- Trim response body to 8KB max for readability, appending "..." if truncated

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 29, 2026

Should we trim the body if it's too large for readability in the normal case where the error body isn't that interesting and just represents an outage or something like that?

Added trimming - response bodies are now limited to 8KB max, with "..." appended if truncated. (1d7fe0c)

halter73
halter73 previously approved these changes Jan 29, 2026
@stephentoub stephentoub enabled auto-merge (squash) January 29, 2026 19:29
1KB is sufficient to capture typical error messages while improving readability.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
auto-merge was automatically disabled January 29, 2026 19:34

Head branch was pushed to by a user without write access

Copilot AI requested a review from stephentoub January 29, 2026 19:34
@stephentoub stephentoub requested a review from halter73 January 29, 2026 20:26
@stephentoub stephentoub enabled auto-merge (squash) January 29, 2026 20:26
@stephentoub stephentoub disabled auto-merge January 29, 2026 22:23
@stephentoub stephentoub merged commit 7c85ac6 into main Jan 29, 2026
9 of 10 checks passed
@stephentoub stephentoub deleted the copilot/update-error-handling-transport-clients branch January 29, 2026 22:23
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.

3 participants