From fbc7400524db3237b930e9577cb08755fe00f5e9 Mon Sep 17 00:00:00 2001 From: Jedd Morgan <45512892+JR-Morgan@users.noreply.github.com> Date: Tue, 19 Nov 2024 10:55:27 +0000 Subject: [PATCH] Changed http handler to not throw on non-successful results (#170) Co-authored-by: Adam Hathcock --- .../SpeckleHttpClientHandler.cs | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/Speckle.Sdk.Dependencies/SpeckleHttpClientHandler.cs b/src/Speckle.Sdk.Dependencies/SpeckleHttpClientHandler.cs index 269edf1a..2070de4e 100644 --- a/src/Speckle.Sdk.Dependencies/SpeckleHttpClientHandler.cs +++ b/src/Speckle.Sdk.Dependencies/SpeckleHttpClientHandler.cs @@ -51,39 +51,30 @@ public sealed class SpeckleHttpClientHandler : DelegatingHandler .ConfigureAwait(false); context.TryGetValue("retryCount", out var retryCount); activity?.SetTag("retryCount", retryCount); - if (policyResult.FinalException != null) - { - activity?.RecordException(policyResult.FinalException); - activity?.SetStatus(SdkActivityStatusCode.Error); - } - else - { - activity?.SetStatus( - policyResult.Result.IsSuccessStatusCode ? SdkActivityStatusCode.Ok : SdkActivityStatusCode.Error - ); - } if (policyResult.Outcome == OutcomeType.Successful) { activity?.SetStatus(SdkActivityStatusCode.Ok); return policyResult.Result; } + activity?.SetStatus(SdkActivityStatusCode.Error); - if (policyResult.FinalException != null) + + if (policyResult.FinalException is null) { - activity?.RecordException(policyResult.FinalException); + // Outcome was not successful, but did not terminate with an exception (e.g. repeated 500 responses) + return policyResult.FinalHandledResult; } + activity?.RecordException(policyResult.FinalException); + // if the policy failed due to a cancellation, AND it was our cancellation token, then don't wrap the exception, and rethrow an new cancellation if (policyResult.FinalException is OperationCanceledException) { cancellationToken.ThrowIfCancellationRequested(); } - throw new HttpRequestException( - "Policy Failed: " + policyResult.FinalHandledResult?.StatusCode ?? "Unknown", - policyResult.FinalException - ); + throw new HttpRequestException("Policy Failed", policyResult.FinalException); } } }