Include response body in HttpRequestException for transport client errors#1193
Merged
stephentoub merged 7 commits intomainfrom Jan 29, 2026
Merged
Conversation
…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 <[email protected]>
Address code review feedback to not swallow cancellation exceptions when reading the response body. Co-authored-by: stephentoub <[email protected]>
stephentoub
reviewed
Jan 28, 2026
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
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 <[email protected]>
stephentoub
reviewed
Jan 29, 2026
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 <[email protected]>
halter73
previously approved these changes
Jan 29, 2026
Contributor
halter73
left a comment
There was a problem hiding this comment.
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?
Contributor
- Remove comment about timeout as suggested by @halter73 - Trim response body to 8KB max for readability, appending "..." if truncated Co-authored-by: stephentoub <[email protected]>
Contributor
Author
Added trimming - response bodies are now limited to 8KB max, with "..." appended if truncated. (1d7fe0c) |
halter73
previously approved these changes
Jan 29, 2026
stephentoub
reviewed
Jan 29, 2026
1KB is sufficient to capture typical error messages while improving readability. Co-authored-by: stephentoub <[email protected]>
auto-merge was automatically disabled
January 29, 2026 19:34
Head branch was pushed to by a user without write access
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
EnsureSuccessStatusCodethrows 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
HttpResponseMessageExtensionsinsrc/Common/:EnsureSuccessStatusCodeWithResponseBodyAsync()- reads body before throwing (with 5-second timeout to prevent hangs)CreateHttpRequestException()- constructs exception with body in messageUpdated transport clients to use the new method:
SseClientSessionTransport(connect + send)StreamableHttpClientSessionTransport(send)ClientOAuthProvider(token exchange + metadata fetch)Result
Before:
After:
HttpRequestException.StatusCodeis 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.