fix - error showing issue

This commit is contained in:
2025-12-13 02:21:57 +05:30
parent d6ab6a0b2b
commit 587749144b
2 changed files with 25 additions and 13 deletions

View File

@@ -428,23 +428,23 @@ export function DdmaEligibilityButton({
// If apiRequest threw, we would have caught above; but just in case it returns. // If apiRequest threw, we would have caught above; but just in case it returns.
let result: any = null; let result: any = null;
let bodyText = ""; let backendError: string | null = null;
try { try {
// attempt JSON first
result = await response.clone().json(); result = await response.clone().json();
backendError =
result?.error || result?.message || result?.detail || null;
} catch { } catch {
// fallback to text response
try { try {
bodyText = await response.clone().text(); const text = await response.clone().text();
} catch {} backendError = text?.trim() || null;
} catch {
backendError = null;
}
} }
const backendError =
result?.error ||
result?.message ||
result?.detail ||
(bodyText && bodyText.trim()) ||
null;
if (!response.ok) { if (!response.ok) {
throw new Error( throw new Error(
backendError || backendError ||

View File

@@ -15,13 +15,25 @@ async function throwIfResNotOk(res: Response) {
// Try to parse the response as JSON for a more meaningful error message // Try to parse the response as JSON for a more meaningful error message
let message = `${res.status}: ${res.statusText}`; let message = `${res.status}: ${res.statusText}`;
try { try {
const errorBody = await res.json(); const errorBody = await res.clone().json();
if (errorBody?.message) {
if (errorBody?.error) {
message = errorBody.error;
} else if (errorBody?.message) {
message = errorBody.message; message = errorBody.message;
} else if (errorBody?.detail) {
message = errorBody.detail;
} }
} catch { } catch {
// ignore JSON parse errors, keep default message // fallback to reading raw text so no error is lost
try {
const text = await res.clone().text();
if (text?.trim()) {
message = text.trim();
}
} catch {}
} }
throw new Error(message); throw new Error(message);