Skip to content

Commit 35fa849

Browse files
committed
req: Improves error handling in API client
Enhances error message construction to include error codes and details. Modifies the error handling logic to prioritize specific error messages when available, falling back to a generic message if necessary. Refactors the ErrorContainer class to include dedicated properties for error details and error codes, enabling more granular error reporting.
1 parent 307c439 commit 35fa849

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

libs/HyperGuestSDK/ApiClient.cs

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,13 @@ protected internal (HttpRequestMessage, string?) CreateHttpRequest<TRequest>(
308308
{
309309
var result = JsonSerializer.Deserialize<ErrorContainer>(stringContent, _deserializerOptions);
310310

311-
if (result?.Message is not { Length: > 0 })
311+
if (result?.Error is not { Length: > 0 })
312312
{
313-
error = new(Resources.ApiClient_UnknownResponse, result?.Errors);
313+
error = new(Resources.ApiClient_UnknownResponse, result?.ToErrorDictionary());
314314
}
315315
else
316316
{
317-
error = new(result.Message, result.Errors);
317+
error = new(BuildErrorMessage(result!), result?.ToErrorDictionary());
318318
}
319319
}
320320
else
@@ -403,13 +403,13 @@ protected internal (HttpRequestMessage, string?) CreateHttpRequest<TRequest>(
403403
{
404404
var result = JsonSerializer.Deserialize<ErrorContainer>(stringContent, _deserializerOptions);
405405

406-
if (result?.Message is not { Length: > 0 })
406+
if (result?.Error is not { Length: > 0 })
407407
{
408-
error = new(Resources.ApiClient_UnknownResponse, result?.Errors);
408+
error = new(Resources.ApiClient_UnknownResponse, result?.ToErrorDictionary());
409409
}
410410
else
411411
{
412-
error = new(result.Message, result.Errors);
412+
error = new(BuildErrorMessage(result!), result?.ToErrorDictionary());
413413
}
414414
}
415415
catch (Exception ex)
@@ -442,18 +442,68 @@ protected internal (HttpRequestMessage, string?) CreateHttpRequest<TRequest>(
442442
? new RateLimiting { Limit = limit, Remaining = remaining } : null;
443443
}
444444

445+
string BuildErrorMessage(ErrorContainer container)
446+
{
447+
var builder = new StringBuilder();
448+
if (container.ErrorCode is { Length: > 0 })
449+
{
450+
builder.Append(container.ErrorCode);
451+
builder.Append(": ");
452+
}
453+
454+
if (container.Error is { Length: > 0 })
455+
{
456+
builder.Append(container.Error);
457+
builder.Append("; ");
458+
}
459+
460+
if (container.ErrorDetails is { Length: > 0 })
461+
{
462+
builder.Append(string.Join("; ", container.ErrorDetails.Select(ed => ed.Message).Where(m => m is { Length: > 0 })));
463+
}
464+
465+
return builder.ToString().TrimEnd(' ', ';', ':') is { Length: > 0 } msg
466+
? msg
467+
: Resources.ApiClient_UnknownResponse;
468+
}
469+
445470
string? GetHeader(string name, HttpHeaders headers)
446471
=> headers.TryGetValues(name, out var values)
447472
? values.First()
448473
: null;
449474

450475
class ErrorContainer
451476
{
452-
[JsonPropertyName("errors")]
453-
public Dictionary<string, string[]>? Errors { get; set; }
477+
[JsonPropertyName("error")]
478+
public string? Error { get; set; }
454479

480+
[JsonPropertyName("errorCode")]
481+
public string? ErrorCode { get; set; }
482+
483+
[JsonPropertyName("errorDetails")]
484+
public ErrorDetails[]? ErrorDetails { get; set; }
485+
486+
[JsonPropertyName("bookingId")]
487+
public int? BookingId { get; set; }
488+
489+
public Dictionary<string, string[]>? ToErrorDictionary()
490+
{
491+
if (ErrorDetails is null)
492+
{
493+
return null;
494+
}
495+
496+
var dict = new Dictionary<string, string[]>();
497+
dict.Add(ErrorCode ?? "error", ErrorDetails.Select(ed => ed.Message ?? string.Empty).ToArray());
498+
499+
return dict;
500+
}
501+
}
502+
503+
public class ErrorDetails
504+
{
455505
[JsonPropertyName("message")]
456-
public string Message { get; set; } = default!;
506+
public string? Message { get; set; }
457507
}
458508
#endregion
459509

0 commit comments

Comments
 (0)