From cc5835652735ca9049f3e6ec83459612e2c3eb51 Mon Sep 17 00:00:00 2001 From: Matthew Abbott Date: Fri, 16 Feb 2024 12:27:03 +0000 Subject: [PATCH] fix: Capture response content if we catch an exception and content is available --- libs/TrybeSDK/ApiClient.cs | 76 ++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/libs/TrybeSDK/ApiClient.cs b/libs/TrybeSDK/ApiClient.cs index ae7d864..b8d84a1 100644 --- a/libs/TrybeSDK/ApiClient.cs +++ b/libs/TrybeSDK/ApiClient.cs @@ -34,10 +34,11 @@ protected internal async Task 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( @@ -62,12 +63,26 @@ protected internal async Task 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; } } @@ -78,10 +93,11 @@ protected internal async Task 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); var transformedResponse = await TransformResponse( httpReq.Method, @@ -105,12 +121,26 @@ protected internal async Task 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; } } @@ -121,10 +151,11 @@ protected internal async Task> FetchAsync( { 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( @@ -149,12 +180,26 @@ protected internal async Task> FetchAsync( } 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; } } @@ -166,10 +211,11 @@ protected internal async Task> FetchAsync( @@ -194,12 +240,26 @@ protected internal async Task> FetchAsync( + 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; } } #endregion