Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/ConfigCat.Cli.Services/Api/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected async Task<TResult> GetAsync<TResult>(HttpMethod method, string path,
var content = await response.Content.ReadAsStringAsync(token);
this.Output.Verbose($"Response body: {content}");

ValidateResponse(response);
ValidateResponse(response, content);

try
{
Expand Down Expand Up @@ -96,7 +96,7 @@ protected async Task SendAsync(HttpMethod method, string path, object body, Canc
var content = await response.Content.ReadAsStringAsync(token);
this.Output.Verbose($"Response body: {content}");

ValidateResponse(response);
ValidateResponse(response, content);
}

protected async Task<TResult> SendAsync<TResult>(HttpMethod method, string path, object body, CancellationToken token)
Expand All @@ -119,7 +119,7 @@ protected async Task<TResult> SendAsync<TResult>(HttpMethod method, string path,
var content = await response.Content.ReadAsStringAsync(token);
this.Output.Verbose($"Response body: {content}");

ValidateResponse(response);
ValidateResponse(response, content);

try
{
Expand Down Expand Up @@ -160,10 +160,17 @@ private void LogRetry(HttpResponseMessage result, Exception exception, AttemptCo
this.Output.Verbose($"{message}, retrying... [{context.CurrentAttempt}. attempt, waiting {context.CurrentDelay}]", ConsoleColor.Yellow);
}

private static void ValidateResponse(HttpResponseMessage responseMessage)
private static void ValidateResponse(HttpResponseMessage responseMessage, string content)
{
if (!responseMessage.IsSuccessStatusCode)
{
Dictionary<string, string[]> errorDetails;
try { errorDetails = JsonSerializer.Deserialize<Dictionary<string, string[]>>(content); }
catch { errorDetails = null; }

throw new HttpStatusException(responseMessage.StatusCode,
responseMessage.ReasonPhrase);
responseMessage.ReasonPhrase,
errorDetails);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using System;
using System.Collections.Generic;
using System.Net;

namespace ConfigCat.Cli.Services.Exceptions;

public class HttpStatusException(
HttpStatusCode statusCode,
string reason,
Dictionary<string, string[]> errorDetails = null,
string message = null,
Exception innerException = null)
: Exception(message, innerException)
{
public HttpStatusCode StatusCode { get; } = statusCode;

public string ReasonPhrase { get; } = reason;

public IReadOnlyDictionary <string, string[]> ErrorDetails { get; } = errorDetails;
}
11 changes: 10 additions & 1 deletion src/ConfigCat.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
Expand Down Expand Up @@ -105,7 +106,15 @@ private static async Task<int> Main(string[] args)
output.WriteError("Terminated.");
break;
case HttpStatusException statusException:
output.WriteError($"Http request failed: {(int)statusException.StatusCode} {statusException.ReasonPhrase}.");
var errorMessage = $"Http request failed: {(int)statusException.StatusCode} {statusException.ReasonPhrase}.";
if (statusException.ErrorDetails is { } errorDetails)
{
errorMessage += Environment.NewLine
+ "Details:" + Environment.NewLine
+ string.Join(Environment.NewLine, errorDetails
.SelectMany(kvp => kvp.Value.Select(msg => $"- [{kvp.Key}] {msg}")));
}
output.WriteError(errorMessage);
break;
case MaxRetryAttemptsReachedException { OperationResult: HttpResponseMessage response }:
output.WriteError($"Http request failed: {(int)response.StatusCode} {response.ReasonPhrase}.");
Expand Down