diff --git a/src/api/client.js b/src/api/client.js index a460fe2..bfeaa1e 100644 --- a/src/api/client.js +++ b/src/api/client.js @@ -17,8 +17,19 @@ class ApiClient { const res = await fetch(`${API_BASE_URL}${endpoint}`, { ...options, headers }); const text = await res.text(); let data = null; - try { data = text ? JSON.parse(text) : null; } catch { throw new Error('Ungültige Server-Antwort'); } - if (!res.ok) throw new Error(data?.error || `HTTP ${res.status}`); + try { + 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; }