Skip to content

Commit

Permalink
Update displayed error message for enterprise policies (#405)
Browse files Browse the repository at this point in the history
* update displayed error message
  • Loading branch information
neelip committed Feb 27, 2024
1 parent cc62516 commit 10c2848
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions cs/src/Management/TunnelManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Authentication;
using System.Text.Json;

#if NET5_0_OR_GREATER
using System.Net.Http.Json;
Expand Down Expand Up @@ -690,12 +691,29 @@ 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 message = response.Content != null ? await response.Content.ReadAsStringAsync() : string.Empty;
errorMessage = message;
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;
}

var ex = new UnauthorizedAccessException(errorMessage, hrex);
Expand Down Expand Up @@ -739,10 +757,11 @@ private string UserLimitsPath
/// <remarks>
/// Copied from Microsoft.VsSaaS.Common to avoid taking a dependency on that assembly.
/// </remarks>
private class ErrorDetails
{
public string? Message { get; set; }
public string? StackTrace { get; set; }
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 10c2848

Please sign in to comment.