fix - error showing issue
This commit is contained in:
@@ -428,22 +428,22 @@ export function DdmaEligibilityButton({
|
||||
|
||||
// If apiRequest threw, we would have caught above; but just in case it returns.
|
||||
let result: any = null;
|
||||
let bodyText = "";
|
||||
let backendError: string | null = null;
|
||||
|
||||
try {
|
||||
// attempt JSON first
|
||||
result = await response.clone().json();
|
||||
backendError =
|
||||
result?.error || result?.message || result?.detail || null;
|
||||
} catch {
|
||||
// fallback to text response
|
||||
try {
|
||||
bodyText = await response.clone().text();
|
||||
} catch {}
|
||||
const text = await response.clone().text();
|
||||
backendError = text?.trim() || null;
|
||||
} catch {
|
||||
backendError = null;
|
||||
}
|
||||
}
|
||||
|
||||
const backendError =
|
||||
result?.error ||
|
||||
result?.message ||
|
||||
result?.detail ||
|
||||
(bodyText && bodyText.trim()) ||
|
||||
null;
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
|
||||
@@ -15,13 +15,25 @@ async function throwIfResNotOk(res: Response) {
|
||||
|
||||
// Try to parse the response as JSON for a more meaningful error message
|
||||
let message = `${res.status}: ${res.statusText}`;
|
||||
|
||||
try {
|
||||
const errorBody = await res.json();
|
||||
if (errorBody?.message) {
|
||||
const errorBody = await res.clone().json();
|
||||
|
||||
if (errorBody?.error) {
|
||||
message = errorBody.error;
|
||||
} else if (errorBody?.message) {
|
||||
message = errorBody.message;
|
||||
} else if (errorBody?.detail) {
|
||||
message = errorBody.detail;
|
||||
}
|
||||
} 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);
|
||||
|
||||
Reference in New Issue
Block a user