From be37dff5ee1a96de24b6c6bba471a003473a4c97 Mon Sep 17 00:00:00 2001 From: Neelima Potharaj Date: Thu, 7 Mar 2024 15:25:47 -0800 Subject: [PATCH] update response handling --- cs/src/Management/TunnelManagementClient.cs | 42 +++++++-------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/cs/src/Management/TunnelManagementClient.cs b/cs/src/Management/TunnelManagementClient.cs index 4a6d0a89..b3ef142e 100644 --- a/cs/src/Management/TunnelManagementClient.cs +++ b/cs/src/Management/TunnelManagementClient.cs @@ -636,10 +636,19 @@ private string UserLimitsPath problemDetails.Detail == null) { return default; + } + + // Enterprise Policies + if (response.Headers.Contains("X-Enterprise-Policy-Failure")) + { + errorMessage = problemDetails!.Title + ": " + problemDetails.Detail; + } + else + { + errorMessage = "Tunnel service error: " + + problemDetails!.Title + " " + problemDetails.Detail; } - - errorMessage = "Tunnel service error: " + - problemDetails!.Title + " " + problemDetails.Detail; + if (problemDetails.Errors != null) { foreach (var error in problemDetails.Errors) @@ -691,31 +700,7 @@ private string UserLimitsPath throw new ArgumentException(errorMessage, hrex); case HttpStatusCode.Unauthorized: - case HttpStatusCode.Forbidden: - // Enterprise Policies - if (response.Headers.Contains("X-Enterprise-Policy-Failure")) - { - var options = new JsonSerializerOptions - { - PropertyNameCaseInsensitive = true - }; - var message = response.Content != null ? await response.Content.ReadAsStringAsync() : string.Empty; - - ErrorDetails? errorDetails = null; - try - { - errorDetails = JsonSerializer.Deserialize(message, options); - } - catch (JsonException) - { - // If deserialization fails, it means the message is not in JSON format. - // In this case, use the message directly as the error message. - } - - // Use the deserialized error detail if available, otherwise use the raw message. - errorMessage = errorDetails?.Detail ?? message; - } - + case HttpStatusCode.Forbidden: var ex = new UnauthorizedAccessException(errorMessage, hrex); // The HttpResponseHeaders.WwwAuthenticate property does not correctly @@ -761,7 +746,6 @@ private class ErrorDetails { public string? Message { get; set; } public string? StackTrace { get; set; } - public string? Detail { get; set; } } ///