Skip to content

Commit d02de15

Browse files
authored
Improve error reporting (#55)
* Improve error reporting
1 parent 647b0c9 commit d02de15

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

‎src/ConfigCat.Cli.Services/Api/ApiClient.cs‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected async Task<TResult> GetAsync<TResult>(HttpMethod method, string path,
6464
var content = await response.Content.ReadAsStringAsync(token);
6565
this.Output.Verbose($"Response body: {content}");
6666

67-
ValidateResponse(response);
67+
ValidateResponse(response, content);
6868

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

99-
ValidateResponse(response);
99+
ValidateResponse(response, content);
100100
}
101101

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

122-
ValidateResponse(response);
122+
ValidateResponse(response, content);
123123

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

163-
private static void ValidateResponse(HttpResponseMessage responseMessage)
163+
private static void ValidateResponse(HttpResponseMessage responseMessage, string content)
164164
{
165165
if (!responseMessage.IsSuccessStatusCode)
166+
{
167+
Dictionary<string, string[]> errorDetails;
168+
try { errorDetails = JsonSerializer.Deserialize<Dictionary<string, string[]>>(content); }
169+
catch { errorDetails = null; }
170+
166171
throw new HttpStatusException(responseMessage.StatusCode,
167-
responseMessage.ReasonPhrase);
172+
responseMessage.ReasonPhrase,
173+
errorDetails);
174+
}
168175
}
169176
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Net;
34

45
namespace ConfigCat.Cli.Services.Exceptions;
56

67
public class HttpStatusException(
78
HttpStatusCode statusCode,
89
string reason,
10+
IReadOnlyDictionary<string, string[]> errorDetails = null,
911
string message = null,
1012
Exception innerException = null)
1113
: Exception(message, innerException)
1214
{
1315
public HttpStatusCode StatusCode { get; } = statusCode;
1416

1517
public string ReasonPhrase { get; } = reason;
18+
19+
public IReadOnlyDictionary <string, string[]> ErrorDetails { get; } = errorDetails;
1620
}

‎src/ConfigCat.Cli/Program.cs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.CommandLine.IO;
1616
using System.CommandLine.Parsing;
1717
using System.Diagnostics.CodeAnalysis;
18+
using System.Linq;
1819
using System.Net.Http;
1920
using System.Reflection;
2021
using System.Security.Cryptography;
@@ -105,7 +106,15 @@ private static async Task<int> Main(string[] args)
105106
output.WriteError("Terminated.");
106107
break;
107108
case HttpStatusException statusException:
108-
output.WriteError($"Http request failed: {(int)statusException.StatusCode} {statusException.ReasonPhrase}.");
109+
var errorMessage = $"Http request failed: {(int)statusException.StatusCode} {statusException.ReasonPhrase}.";
110+
if (statusException.ErrorDetails is { } errorDetails)
111+
{
112+
errorMessage += Environment.NewLine
113+
+ "Details:" + Environment.NewLine
114+
+ string.Join(Environment.NewLine, errorDetails
115+
.SelectMany(kvp => kvp.Value.Select(msg => $"- [{kvp.Key}] {msg}")));
116+
}
117+
output.WriteError(errorMessage);
109118
break;
110119
case MaxRetryAttemptsReachedException { OperationResult: HttpResponseMessage response }:
111120
output.WriteError($"Http request failed: {(int)response.StatusCode} {response.ReasonPhrase}.");

0 commit comments

Comments
 (0)