Skip to content

Commit

Permalink
update response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
neelip committed Mar 7, 2024
1 parent 8cae9b2 commit be37dff
Showing 1 changed file with 13 additions and 29 deletions.
42 changes: 13 additions & 29 deletions cs/src/Management/TunnelManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<ErrorDetails>(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
Expand Down Expand Up @@ -761,7 +746,6 @@ private class ErrorDetails
{
public string? Message { get; set; }
public string? StackTrace { get; set; }
public string? Detail { get; set; }
}

/// <inheritdoc/>
Expand Down

0 comments on commit be37dff

Please sign in to comment.