Skip to content

Commit 94b45b5

Browse files
committed
Improve error handling and add new test case
Enhanced error handling in TlsClient.Core with a try-catch block and default response creation for exceptions. Refined response handling in TlsClient.HttpClient by replacing status checks with `IsSuccessStatus` and adding null/empty validations. Added a new test case `ShouldErrorWithCatch` in TlsClient.Issues to validate error handling for 500 Internal Server Error responses. Updated base URL and request path in Issue4.cs to use `https://httpbin.org` and simulate error scenarios. General improvements include additional null/empty checks and increased test coverage.
1 parent 862273f commit 94b45b5

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

src/TlsClient.Core/TlsClient.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,26 @@ public async Task<Response> RequestAsync(Request request, CancellationToken canc
6767
request.Headers.Add(header.Key, header.Value[0]);
6868
}
6969
}
70-
var rawResponse= await _wrapper.RequestAsync(RequestHelpers.Prepare(request), cancellationToken);
71-
var response = JsonConvert.DeserializeObject<Response>(rawResponse) ?? throw new Exception("Response is null, can't convert object from json.");
72-
73-
// Need to free memory, because the native library allocates memory for the response
74-
await _wrapper.FreeMemoryAsync(response.Id, cancellationToken);
70+
71+
Response response;
72+
73+
try
74+
{
75+
var rawResponse = await _wrapper.RequestAsync(RequestHelpers.Prepare(request), cancellationToken);
76+
response = JsonConvert.DeserializeObject<Response>(rawResponse) ?? throw new Exception("Response is null, can't convert object from json.");
77+
}
78+
catch(Exception err)
79+
{
80+
response = new Response()
81+
{
82+
Body = err.Message,
83+
Status = 0,
84+
};
85+
}
86+
87+
if(!string.IsNullOrEmpty(response.Id))
88+
await _wrapper.FreeMemoryAsync(response.Id, cancellationToken);
89+
7590
return response;
7691
}
7792

src/TlsClient.HttpClient/TlsClientHandler.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
7676
RequestMessage = request,
7777
};
7878

79-
if (!string.IsNullOrWhiteSpace(response.Body) && response.Status!=0)
79+
if (!string.IsNullOrWhiteSpace(response.Body) && response.IsSuccessStatus)
8080
{
8181
var parsed = response.Body.ToParsedBase64();
82-
httpResponseMessage.Content = new ByteArrayContent(Convert.FromBase64String(parsed.Item2));
83-
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(parsed.Item1);
82+
if(!string.IsNullOrEmpty(parsed.Item1) && !string.IsNullOrEmpty(parsed.Item2))
83+
{
84+
httpResponseMessage.Content = new ByteArrayContent(Convert.FromBase64String(parsed.Item2));
85+
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(parsed.Item1);
86+
}
8487
}
8588
else
8689
{

tests/TlsClient.Issues/Issue4.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,29 @@ namespace TlsClient.Issues
1010
{
1111
public class Issue4
1212
{
13+
[Fact]
14+
public async Task ShouldErrorWithCatch()
15+
{
16+
using var tlsClient = new TlsClientBuilder()
17+
.WithIdentifier(TlsClientIdentifier.Chrome132)
18+
.WithUserAgent("TestClient 1.0")
19+
.WithFollowRedirects(true)
20+
.WithLibraryPath("D:\\Tools\\TlsClient\\tls-client-windows-64-1.9.1.dll")
21+
.WithTimeout(TimeSpan.FromSeconds(10))
22+
.Build();
23+
24+
25+
var restClient = new TlsRestClientBuilder()
26+
.WithBaseUrl("https://httpbin.org")
27+
.WithTlsClient(tlsClient)
28+
.Build();
29+
30+
var restReq = new RestRequest("/status/500", Method.Get);
31+
var restResponse = await restClient.ExecuteAsync(restReq);
32+
33+
restResponse.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
34+
}
35+
1336
[Fact]
1437
public async Task ShouldMemoryLeakRestSharp_MultiThreaded()
1538
{
@@ -34,13 +57,13 @@ public async Task ShouldMemoryLeakRestSharp_MultiThreaded()
3457

3558

3659
var restClient = new TlsRestClientBuilder()
37-
.WithBaseUrl("http://example.com")
60+
.WithBaseUrl("https://httpbin.org")
3861
.WithTlsClient(tlsClient)
3962
.Build();
4063

4164
for (int j = 0; j < requestsPerThread; j++)
4265
{
43-
var restReq = new RestRequest("/", Method.Get);
66+
var restReq = new RestRequest("/status/500", Method.Get);
4467
var restResponse = await restClient.ExecuteAsync(restReq);
4568
Console.WriteLine(restResponse.Content);
4669
}

0 commit comments

Comments
 (0)