Skip to content

Commit

Permalink
fix: Capture response content if we catch an exception and content is…
Browse files Browse the repository at this point in the history
… available
  • Loading branch information
Antaris committed Feb 16, 2024
1 parent 59ed99f commit cc58356
Showing 1 changed file with 68 additions and 8 deletions.
76 changes: 68 additions & 8 deletions libs/TrybeSDK/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ protected internal async Task<TrybeResponse> SendAsync(
{
Ensure.IsNotNull(request, nameof(request));
var httpReq = CreateHttpRequest(request);
HttpResponseMessage? httpResp = null;

try
{
var httpResp = await _http.SendAsync(httpReq, cancellationToken)
httpResp = await _http.SendAsync(httpReq, cancellationToken)
.ConfigureAwait(false);

var transformedResponse = await TransformResponse(
Expand All @@ -62,12 +63,26 @@ protected internal async Task<TrybeResponse> SendAsync(
}
catch (Exception ex)
{
return new TrybeResponse(
var response = new TrybeResponse(
httpReq.Method,
httpReq.RequestUri,
false,
(HttpStatusCode)0,
error: new Error(ex.Message, exception: ex));

if (httpReq?.Content is not null)
{
response.RequestContent = await httpReq.Content.ReadAsStringAsync()
.ConfigureAwait(false);
}

if (httpResp?.Content is not null)
{
response.ResponseContent = await httpResp.Content.ReadAsStringAsync()
.ConfigureAwait(false); ;
}

return response;
}
}

Expand All @@ -78,10 +93,11 @@ protected internal async Task<TrybeResponse> SendAsync<TRequest>(
{
Ensure.IsNotNull(request, nameof(request));
var httpReq = CreateHttpRequest(request);
HttpResponseMessage? httpResp = null;

try
{
var httpResp = await _http.SendAsync(httpReq, cancellationToken);
httpResp = await _http.SendAsync(httpReq, cancellationToken);

var transformedResponse = await TransformResponse(
httpReq.Method,
Expand All @@ -105,12 +121,26 @@ protected internal async Task<TrybeResponse> SendAsync<TRequest>(
}
catch (Exception ex)
{
return new TrybeResponse(
var response = new TrybeResponse(
httpReq.Method,
httpReq.RequestUri,
false,
(HttpStatusCode)0,
error: new Error(ex.Message, exception: ex));

if (httpReq?.Content is not null)
{
response.RequestContent = await httpReq.Content.ReadAsStringAsync()
.ConfigureAwait(false);
}

if (httpResp?.Content is not null)
{
response.ResponseContent = await httpResp.Content.ReadAsStringAsync()
.ConfigureAwait(false); ;
}

return response;
}
}

Expand All @@ -121,10 +151,11 @@ protected internal async Task<TrybeResponse<TResponse>> FetchAsync<TResponse>(
{
Ensure.IsNotNull(request, nameof(request));
var httpReq = CreateHttpRequest(request);
HttpResponseMessage? httpResp = null;

try
{
var httpResp = await _http.SendAsync(httpReq, cancellationToken)
httpResp = await _http.SendAsync(httpReq, cancellationToken)
.ConfigureAwait(false);

var transformedResponse = await TransformResponse<TResponse>(
Expand All @@ -149,12 +180,26 @@ protected internal async Task<TrybeResponse<TResponse>> FetchAsync<TResponse>(
}
catch (Exception ex)
{
return new TrybeResponse<TResponse>(
var response = new TrybeResponse<TResponse>(
httpReq.Method,
httpReq.RequestUri,
false,
(HttpStatusCode)0,
error: new Error(ex.Message, exception: ex));

if (httpReq?.Content is not null)
{
response.RequestContent = await httpReq.Content.ReadAsStringAsync()
.ConfigureAwait(false);
}

if (httpResp?.Content is not null)
{
response.ResponseContent = await httpResp.Content.ReadAsStringAsync()
.ConfigureAwait(false); ;
}

return response;
}
}

Expand All @@ -166,10 +211,11 @@ protected internal async Task<TrybeResponse<TResponse>> FetchAsync<TRequest, TRe
{
Ensure.IsNotNull(request, nameof(request));
var httpReq = CreateHttpRequest(request);
HttpResponseMessage? httpResp = null;

try
{
var httpResp = await _http.SendAsync(httpReq, cancellationToken)
httpResp = await _http.SendAsync(httpReq, cancellationToken)
.ConfigureAwait(false);

var transformedResponse = await TransformResponse<TResponse>(
Expand All @@ -194,12 +240,26 @@ protected internal async Task<TrybeResponse<TResponse>> FetchAsync<TRequest, TRe
}
catch (Exception ex)
{
return new TrybeResponse<TResponse>(
var response = new TrybeResponse<TResponse>(
httpReq.Method,
httpReq.RequestUri,
false,
(HttpStatusCode)0,
error: new Error(ex.Message, exception: ex));

if (httpReq?.Content is not null)
{
response.RequestContent = await httpReq.Content.ReadAsStringAsync()
.ConfigureAwait(false);
}

if (httpResp?.Content is not null)
{
response.ResponseContent = await httpResp.Content.ReadAsStringAsync()
.ConfigureAwait(false); ;
}

return response;
}
}
#endregion
Expand Down

0 comments on commit cc58356

Please sign in to comment.