Centralize API error handling in ApiClientHelper

Refactored error handling logic for API responses into a new static ApiClientHelper class, consolidating the ReadErrorAsync method and ProblemDetailsDto. Updated CatalogApiClient and MassDataApiClient to use the shared helper, removing redundant local methods and classes. This change improves consistency, reduces duplication, and streamlines error message formatting across the application.
This commit is contained in:
OlgunR
2026-04-15 13:55:57 +02:00
parent 39cb63a78d
commit a0e0d7ed03
3 changed files with 64 additions and 86 deletions

View File

@@ -45,27 +45,10 @@ public class MassDataApiClient
return ApiResult<MassDataReadDto?>.Ok(payload);
}
var error = await ReadErrorAsync(response);
var error = await ApiClientHelper.ReadErrorAsync(response);
return ApiResult<MassDataReadDto?>.Fail(error);
}
private static async Task<string> ReadErrorAsync(HttpResponseMessage response)
{
try
{
var problem = await response.Content.ReadFromJsonAsync<ProblemDetailsDto>();
if (problem != null && !string.IsNullOrWhiteSpace(problem.Title))
return problem.Detail ?? problem.Title ?? response.ReasonPhrase ?? "Unbekannter Fehler";
}
catch { }
var body = await response.Content.ReadAsStringAsync();
if (!string.IsNullOrWhiteSpace(body))
return body;
return $"Fehler {(int)response.StatusCode} {response.ReasonPhrase}".Trim();
}
public async Task<MassDataReadDto?> GetByCustomerNameAsync(string customerName)
{
if (string.IsNullOrWhiteSpace(customerName))