Return Statusmesssage from API

This commit is contained in:
2025-12-17 12:14:15 +01:00
parent 76eb71946f
commit bb5aac2944

View File

@@ -54,8 +54,30 @@ internal sealed class ApiClient : IApiClient {
using var res = await _http.SendAsync(req, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false);
var text = await res.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
if (!res.IsSuccessStatusCode)
throw ApiException.From(res.StatusCode, TryGetMessage(text), text);
//if (!res.IsSuccessStatusCode)
// throw ApiException.From(res.StatusCode, TryGetMessage(text), text);
if (res.StatusCode != System.Net.HttpStatusCode.OK) {
// Verhalten wie in BaseFunc: bei Fehlerstatus -> "message" aus Body lesen und mit dessen Inhalt eine Exception werfen.
try {
var options = new JsonDocumentOptions { AllowTrailingCommas = true };
using var doc = JsonDocument.Parse(text, options);
var root = doc.RootElement;
// GetProperty wirft, wenn "message" fehlt — das entspricht dem bisherigen Verhalten in BaseFunc.
var messageElement = root.GetProperty("message");
if (messageElement.ValueKind != JsonValueKind.String)
throw ApiException.From(res.StatusCode, "Fehler: 'message' ist null.", text);
var message = messageElement.GetString() ?? throw ApiException.From(res.StatusCode, "Fehler: 'message' ist null.", text);
throw ApiException.From(res.StatusCode, message, text);
} catch (ApiException) {
throw;
} catch (Exception) {
// Fallback: Wenn Parsing fehlschlägt oder "message" fehlt, konsistente Fehlermeldung wie BaseFunc
throw ApiException.From(res.StatusCode, "Fehler: 'message' ist null.", text);
}
}
if (typeof(T) == typeof(void) || typeof(T) == typeof(object) || string.IsNullOrWhiteSpace(text))
return default!;