fix(api-client): aussagekraeftige Fehlermeldungen
- JSON-Parse-Fehler zeigt jetzt HTTP-Status + Body-Snippet - Validation-Errors (express-validator errors-Array) werden lesbar als Eingabe ungueltig: msg1, msg2 ausgegeben statt HTTP 400
This commit is contained in:
@@ -17,8 +17,19 @@ class ApiClient {
|
|||||||
const res = await fetch(`${API_BASE_URL}${endpoint}`, { ...options, headers });
|
const res = await fetch(`${API_BASE_URL}${endpoint}`, { ...options, headers });
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
let data = null;
|
let data = null;
|
||||||
try { data = text ? JSON.parse(text) : null; } catch { throw new Error('Ungültige Server-Antwort'); }
|
try {
|
||||||
if (!res.ok) throw new Error(data?.error || `HTTP ${res.status}`);
|
data = text ? JSON.parse(text) : null;
|
||||||
|
} catch {
|
||||||
|
throw new Error(`Server-Antwort nicht lesbar (HTTP ${res.status}): ${text.slice(0, 120)}`);
|
||||||
|
}
|
||||||
|
if (!res.ok) {
|
||||||
|
// Validation-Errors (express-validator) zeigen
|
||||||
|
if (Array.isArray(data?.errors) && data.errors.length) {
|
||||||
|
const msg = data.errors.map(e => e.msg || e.message || JSON.stringify(e)).join(', ');
|
||||||
|
throw new Error(`Eingabe ungültig: ${msg}`);
|
||||||
|
}
|
||||||
|
throw new Error(data?.error || `HTTP ${res.status}`);
|
||||||
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user