fix(campaigns): correct Axios error parsing in activateMutation
Some checks failed
Aegis CI / lint-and-test (push) Has been cancelled

FastAPI wraps error bodies as {detail: string | object}, not at the
top level. Was reading data.message instead of data.detail.message,
causing [object Object] in the toast for all non-409 errors.

Now correctly extracts:
- 409 with object detail -> start_date warning modal
- Other errors with string detail -> readable toast message
- Other errors with object detail -> detail.message in toast

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
kitos
2026-06-04 15:57:54 +02:00
parent 131817cc81
commit 910c198545

View File

@@ -98,12 +98,29 @@ export default function CampaignDetailPage() {
showToast("Campaign activated", "success"); showToast("Campaign activated", "success");
}, },
onError: (err: unknown) => { onError: (err: unknown) => {
// 409 = future start_date warning → show confirmation modal instead of toast // FastAPI wraps error bodies as: { detail: string | object }
const axiosErr = err as { response?: { status?: number; data?: { message?: string; start_date?: string } } }; type AxiosLike = {
if (axiosErr?.response?.status === 409 && axiosErr.response.data?.message) { response?: {
setStartDateWarning(axiosErr.response.data.message); status?: number;
data?: { detail?: { code?: string; message?: string } | string };
};
};
const axiosErr = err as AxiosLike;
const status = axiosErr?.response?.status;
const detail = axiosErr?.response?.data?.detail;
if (status === 409 && detail && typeof detail === "object" && detail.message) {
// Future start_date → show confirmation modal
setStartDateWarning(detail.message);
} else { } else {
showToast((err as Error).message, "error"); // Any other error → extract readable message from FastAPI detail
const msg =
typeof detail === "string"
? detail
: typeof detail === "object" && detail?.message
? detail.message
: "Failed to activate campaign";
showToast(msg, "error");
} }
}, },
}); });