Type caught errors as unknown in the frontend

Replace `catch (e: any)` with `catch (e)` and read the message through a
new errorMessage() helper. ApiError.data gets a real type, and
useDismissingState's setter keeps one identity across renders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 11:10:00 +02:00
co-authored by Claude Opus 5.5
parent 958a5a5950
commit 619b5b9a93
55 changed files with 400 additions and 332 deletions
@@ -4,6 +4,7 @@ import { useAuth } from "@/hooks/useAuth";
import { apiFetch } from "@/lib/api";
import { useDismissingState } from "@/hooks/useDismissingState";
import { HandHeart } from "lucide-react";
import { errorMessage } from "@/lib/utils";
export default function DonatePage() {
const { token } = useAuth();
@@ -20,8 +21,8 @@ export default function DonatePage() {
const evs = await apiFetch<any[]>("/api/events");
setEvents(evs);
if (evs.length > 0) setEventId(evs[0].id);
} catch (e: any) {
setError(e?.message || "Failed to load events");
} catch (e) {
setError(errorMessage(e) || "Failed to load events");
}
})();
}, []);
@@ -49,8 +50,8 @@ export default function DonatePage() {
});
setInfo("Redirecting to payment...");
window.location.href = res.redirectUrl;
} catch (e: any) {
setError(e?.message || "Failed to start donation checkout");
} catch (e) {
setError(errorMessage(e) || "Failed to start donation checkout");
} finally {
setLoading(false);
}
@@ -6,6 +6,7 @@ import { useAuth } from "@/hooks/useAuth";
import { apiFetch } from "@/lib/api";
import { useDismissingState } from "@/hooks/useDismissingState";
import { FileText } from "lucide-react";
import { errorMessage } from "@/lib/utils";
// Types for form fields
type FormField = { id: string; type: 'yes_no'|'text'|'date'|'numeric'|'statement'|'paragraph'|'rating'|'multiple_choice'; label: string; isRequired?: boolean; helpText?: string|null; options?: unknown };
@@ -41,8 +42,8 @@ function FormsContent() {
const draft = await apiFetch<any>(`/api/registrations/${encodeURIComponent(registrationId)}/forms/draft`, { authToken: token });
if (draft && draft.data && typeof draft.data === 'object') setFormsData(draft.data);
} catch {}
} catch (e: any) {
setError(e?.message || 'Failed to load registration');
} catch (e) {
setError(errorMessage(e) || 'Failed to load registration');
} finally {
setLoading(false);
}
@@ -108,8 +109,8 @@ function FormsContent() {
setFormsData({});
// On success, redirect to the user dashbaord
router.push('/dashboard/user');
} catch (e: any) {
setError(e?.message || 'Failed to submit forms');
} catch (e) {
setError(errorMessage(e) || 'Failed to submit forms');
}
};
@@ -123,8 +124,8 @@ function FormsContent() {
body: { data: formsData }
});
setInfo('Draft saved. You can return later to finish.');
} catch (e: any) {
setError(e?.message || 'Failed to save draft');
} catch (e) {
setError(errorMessage(e) || 'Failed to save draft');
}
};
+20 -19
View File
@@ -12,6 +12,7 @@ import { AttendeesPanel } from "@/components/shared/AttendeesPanel";
import { MyWaitlists } from "@/components/shared/MyWaitlists";
import { TransferTicketModal, canOfferTransfer } from "@/components/shared/TransferTicketModal";
import { ClipboardList, Calendar, Ticket, ChevronRight } from "lucide-react";
import { errorMessage } from "@/lib/utils";
// Helper formatters
const formatRand = (n: number) => `R ${n.toFixed(2)}`;
@@ -178,8 +179,8 @@ export default function UserDashboardPage() {
}
})
);
} catch (e: any) {
setError(e?.message || "Failed to load data");
} catch (e) {
setError(errorMessage(e) || "Failed to load data");
} finally {
setLoading(false);
}
@@ -252,9 +253,9 @@ export default function UserDashboardPage() {
});
const msg = (res && res.message) ? res.message : `${ticketIds.length} ticket(s) emailed successfully.`;
setDialog({ open: true, message: msg, loading: false });
} catch (e: any) {
} catch (e) {
setDialog({ open: false, message: "", loading: false });
setError(e?.message || "Failed to email tickets");
setError(errorMessage(e) || "Failed to email tickets");
}
};
@@ -272,9 +273,9 @@ export default function UserDashboardPage() {
});
const msg = (res && res.message) ? res.message : `${ticketIds.length} ticket(s) sent to WhatsApp.`;
setDialog({ open: true, message: msg, loading: false });
} catch (e: any) {
} catch (e) {
setDialog({ open: false, message: "", loading: false });
setError(e?.message || "Failed to send tickets to WhatsApp");
setError(errorMessage(e) || "Failed to send tickets to WhatsApp");
}
};
@@ -294,9 +295,9 @@ export default function UserDashboardPage() {
authToken: token,
});
setDialog({ open: true, loading: false, title: "Invoice sent", message: (res && res.message) || "Invoice sent." });
} catch (e: any) {
} catch (e) {
setDialog({ open: false, message: "", loading: false });
setError(e?.message || "Failed to send invoice");
setError(errorMessage(e) || "Failed to send invoice");
}
};
@@ -316,9 +317,9 @@ export default function UserDashboardPage() {
authToken: token,
});
setDialog({ open: true, loading: false, title: "Receipt sent", message: (res && res.message) || "Receipt sent." });
} catch (e: any) {
} catch (e) {
setDialog({ open: false, message: "", loading: false });
setError(e?.message || "Failed to send receipt");
setError(errorMessage(e) || "Failed to send receipt");
}
};
@@ -361,9 +362,9 @@ export default function UserDashboardPage() {
return;
}
window.location.href = res.redirectUrl;
} catch (e: any) {
} catch (e) {
setDialog({ open: false, message: "", loading: false });
setError(e?.message || "Failed to create checkout");
setError(errorMessage(e) || "Failed to create checkout");
}
};
@@ -518,8 +519,8 @@ export default function UserDashboardPage() {
} catch {}
setCodeInput("");
setInfo(res?.message || 'Code applied.');
} catch (e: any) {
setCodeError(e?.message || "Couldn't apply that code");
} catch (e) {
setCodeError(errorMessage(e) || "Couldn't apply that code");
} finally {
setCodeBusy(false);
}
@@ -555,8 +556,8 @@ export default function UserDashboardPage() {
setActiveRegId(null);
setShowCancelConfirm(false);
setInfo('Registration cancelled.');
} catch (e: any) {
setError(e?.message || 'Failed to cancel registration');
} catch (e) {
setError(errorMessage(e) || 'Failed to cancel registration');
setShowCancelConfirm(false);
} finally {
setCancelLoading(false);
@@ -718,8 +719,8 @@ export default function UserDashboardPage() {
} catch {}
setInfo('Registration updated successfully');
setEditMode(false);
} catch (e: any) {
setEditError(e?.message || 'Failed to update registration');
} catch (e) {
setEditError(errorMessage(e) || 'Failed to update registration');
} finally {
setLoading(false);
}
@@ -1009,7 +1010,7 @@ export default function UserDashboardPage() {
const r: any = await apiFetch(`/api/tickets/${t.id}/transfer`, { method: "DELETE", authToken: token! });
setInfo(r?.message || "Transfer cancelled");
reloadAfterAttendees();
} catch (e: any) { setError(e?.message || "Couldn't cancel"); }
} catch (e) { setError(errorMessage(e) || "Couldn't cancel"); }
}}>Cancel</button>
</span>
)}
@@ -7,6 +7,7 @@ import { useDismissingState } from "@/hooks/useDismissingState";
import { formatDateTime } from "@/lib/date";
import { formatPaymentMethod } from "@/lib/paymentMethod";
import { Receipt } from "lucide-react";
import { errorMessage } from "@/lib/utils";
interface PaymentItem {
id: string;
@@ -55,8 +56,8 @@ export default function UserPaymentsPage() {
authToken: token,
});
setInfo((res && res.message) || "Receipt sent.");
} catch (e: any) {
setError(e?.message || "Failed to send receipt");
} catch (e) {
setError(errorMessage(e) || "Failed to send receipt");
} finally {
setSendingReceipt(false);
}
@@ -87,8 +88,8 @@ export default function UserPaymentsPage() {
setTotal(res?.total ?? 0);
setTotalPages(res?.pages ?? 1);
setPage(p);
} catch (e: any) {
setError(e?.message || "Failed to load payments");
} catch (e) {
setError(errorMessage(e) || "Failed to load payments");
} finally {
setFetching(false);
}
@@ -8,6 +8,7 @@ import { isValidZAPhone } from "@/lib/phone";
import { useDismissingState } from "@/hooks/useDismissingState";
import { TwoFactorChallenge, TwoFactorMethod } from "@/lib/auth";
import { User, Lock, ShieldAlert, ShieldCheck, ShieldOff, Trash2, Clock, LogIn, KeyRound, RotateCcw } from "lucide-react";
import { errorMessage } from "@/lib/utils";
type ActivityEvent = { id: string; type: "login" | "password_changed" | "password_reset" | "two_factor_enabled" | "two_factor_disabled"; device: string | null; createdAt: string };
@@ -67,8 +68,8 @@ export default function UserProfilePage() {
// Backend returns a refreshed token — save it so the session stays valid
if (res?.token) updateToken(res.token);
setProfileMsg({ type: "ok", text: "Profile updated." });
} catch (e: any) {
setProfileMsg({ type: "err", text: e?.message || "Failed to update profile." });
} catch (e) {
setProfileMsg({ type: "err", text: errorMessage(e) || "Failed to update profile." });
} finally {
setSavingProfile(false);
}
@@ -106,8 +107,8 @@ export default function UserProfilePage() {
setConfirmPassword("");
setPwMsg({ type: "ok", text: "Password changed." });
loadActivity();
} catch (e: any) {
setPwMsg({ type: "err", text: e?.message || "Failed to change password." });
} catch (e) {
setPwMsg({ type: "err", text: errorMessage(e) || "Failed to change password." });
} finally {
setSavingPw(false);
}
@@ -131,8 +132,8 @@ export default function UserProfilePage() {
// save the freshly issued one so this device stays signed in.
if (res?.token) updateToken(res.token);
setRevokeMsg({ type: "ok", text: res?.message || "All other sessions signed out." });
} catch (e: any) {
setRevokeMsg({ type: "err", text: e?.message || "Failed to revoke sessions." });
} catch (e) {
setRevokeMsg({ type: "err", text: errorMessage(e) || "Failed to revoke sessions." });
} finally {
setRevoking(false);
}
@@ -203,8 +204,8 @@ export default function UserProfilePage() {
setTfCode("");
setTfPassword("");
setTfStep("code");
} catch (e: any) {
setTfMsg({ type: "err", text: e?.message || "Failed to send a verification code." });
} catch (e) {
setTfMsg({ type: "err", text: errorMessage(e) || "Failed to send a verification code." });
} finally {
setTfBusy(false);
}
@@ -227,8 +228,8 @@ export default function UserProfilePage() {
resetTwoFactorForm();
setTfMsg({ type: "ok", text: wasEnabled ? `Codes will now be sent by ${TWO_FACTOR_METHOD_LABEL[res.twoFactorMethod]}.` : "Two-factor authentication is on." });
loadActivity();
} catch (e: any) {
setTfMsg({ type: "err", text: e?.message || "Failed to verify the code." });
} catch (e) {
setTfMsg({ type: "err", text: errorMessage(e) || "Failed to verify the code." });
} finally {
setTfBusy(false);
}
@@ -249,8 +250,8 @@ export default function UserProfilePage() {
resetTwoFactorForm();
setTfMsg({ type: "ok", text: "Two-factor authentication is off." });
loadActivity();
} catch (e: any) {
setTfMsg({ type: "err", text: e?.message || "Failed to turn off two-factor authentication." });
} catch (e) {
setTfMsg({ type: "err", text: errorMessage(e) || "Failed to turn off two-factor authentication." });
} finally {
setTfBusy(false);
}
@@ -277,8 +278,8 @@ export default function UserProfilePage() {
setCloseMsg({ type: "ok", text: res?.message || "Account closed." });
// Slight delay so the user can read the message, then log out
setTimeout(() => logout(), 2500);
} catch (e: any) {
setCloseMsg({ type: "err", text: e?.message || "Failed to close account." });
} catch (e) {
setCloseMsg({ type: "err", text: errorMessage(e) || "Failed to close account." });
} finally {
setClosing(false);
}
@@ -5,6 +5,7 @@ import { apiFetch } from "@/lib/api";
import { useDismissingState } from "@/hooks/useDismissingState";
import { useRouter } from "next/navigation";
import { KeyRound } from "lucide-react";
import { errorMessage } from "@/lib/utils";
export default function ResetPasswordPage() {
const { token } = useAuth();
@@ -35,8 +36,8 @@ export default function ResetPasswordPage() {
setPassword("");
setConfirm("");
setTimeout(() => router.push("/dashboard/user"), 1200);
} catch (err: any) {
setError(err?.message || "Failed to update password");
} catch (err) {
setError(errorMessage(err) || "Failed to update password");
} finally {
setLoading(false);
}