-
Notifications
You must be signed in to change notification settings - Fork 677
Include response body in HttpRequestException for transport client errors #1193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stephentoub
merged 7 commits into
main
from
copilot/update-error-handling-transport-clients
Jan 29, 2026
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a3dba75
Initial plan
Copilot b997294
Include response body in HttpRequestException thrown by transport cli…
Copilot c3d93aa
Fix exception handling to allow OperationCanceledException to propagate
Copilot 2c2e43d
Add 5-second timeout when reading error response body
Copilot 0ab4012
Simplify exception handling to catch all exceptions
Copilot 1d7fe0c
Remove superfluous comment and trim large response bodies
Copilot a582e84
Reduce max response body length from 8KB to 1KB
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using System.Net; | ||
| using System.Net.Http; | ||
|
|
||
| namespace ModelContextProtocol; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for <see cref="HttpResponseMessage"/>. | ||
| /// </summary> | ||
| internal static class HttpResponseMessageExtensions | ||
| { | ||
| /// <summary> | ||
| /// Throws an <see cref="HttpRequestException"/> if the <see cref="HttpResponseMessage.IsSuccessStatusCode"/> property is <see langword="false"/>. | ||
| /// Unlike <see cref="HttpResponseMessage.EnsureSuccessStatusCode"/>, this method includes the response body in the exception message | ||
| /// to help diagnose issues when the server returns error details in the response body. | ||
| /// </summary> | ||
| /// <param name="response">The HTTP response message to check.</param> | ||
| /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
| /// <returns>A task that represents the asynchronous operation.</returns> | ||
| /// <exception cref="HttpRequestException">The response status code does not indicate success.</exception> | ||
| public static async Task EnsureSuccessStatusCodeWithResponseBodyAsync(this HttpResponseMessage response, CancellationToken cancellationToken = default) | ||
| { | ||
| if (!response.IsSuccessStatusCode) | ||
| { | ||
| string? responseBody = null; | ||
| try | ||
| { | ||
| // Add a timeout to prevent hanging if the server sends an error response but doesn't end the request. | ||
| using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | ||
| cts.CancelAfter(TimeSpan.FromSeconds(5)); | ||
| responseBody = await response.Content.ReadAsStringAsync(cts.Token).ConfigureAwait(false); | ||
| } | ||
| catch (Exception ex) when (ex is not OperationCanceledException || !cancellationToken.IsCancellationRequested) | ||
stephentoub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| // Ignore errors reading the response body (e.g., stream closed, timeout) - we'll throw without it. | ||
| // Allow cancellation exceptions to propagate only if the original token was cancelled. | ||
| } | ||
|
|
||
| throw CreateHttpRequestException(response, responseBody); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates an <see cref="HttpRequestException"/> for a non-success response, including the response body in the message. | ||
| /// </summary> | ||
| /// <param name="response">The HTTP response message.</param> | ||
| /// <param name="responseBody">The response body content, if available.</param> | ||
| /// <returns>An <see cref="HttpRequestException"/> with the response details.</returns> | ||
| public static HttpRequestException CreateHttpRequestException(HttpResponseMessage response, string? responseBody) | ||
| { | ||
| int statusCodeInt = (int)response.StatusCode; | ||
| string message = string.IsNullOrEmpty(responseBody) | ||
| ? $"Response status code does not indicate success: {statusCodeInt} ({response.ReasonPhrase})." | ||
| : $"Response status code does not indicate success: {statusCodeInt} ({response.ReasonPhrase}). Response body: {responseBody}"; | ||
|
|
||
| #if NET | ||
| return new HttpRequestException(message, inner: null, response.StatusCode); | ||
| #else | ||
| return new HttpRequestException(message); | ||
| #endif | ||
| } | ||
| } | ||
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.