Skip to content

Return fault status code on thrown HTTP client exceptions #20

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions CSharpHTTPClient/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,15 @@ private async Task<Response> RequestAsync(string method, string requestBody = nu
RequestUri = new Uri(endpoint),
Content = content
};
return await MakeRequest(client, request).ConfigureAwait(false);

return await MakeRequest(client, request).ConfigureAwait(false);
}
catch (Exception ex)
{
HttpResponseMessage response = new HttpResponseMessage();
string message;
message = (ex is HttpRequestException) ? ".NET HttpRequestException" : ".NET Exception";
message = message + ", raw message: \n\n";
response.Content = new StringContent(message + ex.Message);
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
var message = ex is HttpRequestException ? ".NET HttpRequestException" : ".NET Exception";
response.Content = new StringContent($"{message}\n{ex}");

return new Response(response.StatusCode, response.Content, response.Headers);
}
}
Expand Down
23 changes: 23 additions & 0 deletions UnitTest/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public async override Task<Response> MakeRequest(HttpClient client, HttpRequestM
}
}

public class MockFailingClient : Client
{
public MockFailingClient(string host, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null) : base(host, requestHeaders, version, urlPath)
{
}

public async override Task<Response> MakeRequest(HttpClient client, HttpRequestMessage request)
{
throw new HttpRequestException("Testing failures");
}
}

[TestFixture]
public class TestClient
{
Expand Down Expand Up @@ -74,5 +86,16 @@ public async void TestMethodCall()
var content = new StringContent("{'test': 'test_content'}", Encoding.UTF8, "application/json");
Assert.AreEqual(response.Body.ReadAsStringAsync().Result, content.ReadAsStringAsync().Result);
}

[Test]
public async void TestFailingMethodCall()
{
var host = "http://api.test.com";
dynamic test_client = new MockFailingClient(host: host);
Response response = await test_client.get();
Assert.IsNotNull(response);
Assert.AreEqual(response.StatusCode, HttpStatusCode.InternalServerError);
Assert.True(response.Body.ReadAsStringAsync().Result.Contains(".NET HttpRequestException"));
}
}
}