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:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user