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 9e8244bb71
commit 5024ab7241
55 changed files with 400 additions and 332 deletions
@@ -7,6 +7,7 @@ import { apiFetch } from "@/lib/api";
import { useDismissingState } from "@/hooks/useDismissingState";
import { History } from "lucide-react";
import { Table, TableHeader, TableBody, TableRow, TableHead, TableCell } from "@/components/ui/table";
import { errorMessage } from "@/lib/utils";
const ACTIONS = [
"refund_created",
@@ -174,8 +175,8 @@ export default function AdminAuditLogPage() {
setEntries(res?.rows || []);
setTotal(res?.total ?? 0);
setPage(p);
} catch (e: any) {
setError(e?.message || "Failed to load audit log");
} catch (e) {
setError(errorMessage(e) || "Failed to load audit log");
} finally {
setFetching(false);
}
@@ -7,6 +7,7 @@ import { apiFetch } from "@/lib/api";
import { useDismissingState } from "@/hooks/useDismissingState";
import type { EventCost, EventCostType, EventFinancials, CashupMethod } from "@/types";
import { Wallet, DollarSign, ClipboardCheck, FileBarChart } from "lucide-react";
import { errorMessage } from "@/lib/utils";
const METHOD_LABELS: Record<CashupMethod, string> = { cash: "Cash", card: "Card", eft: "EFT", other: "Other" };
const METHODS: CashupMethod[] = ["cash", "card", "eft", "other"];
@@ -47,8 +48,8 @@ export default function EventCashupPage() {
]);
setData(financials);
setEventOptions((event.eventOptions || []).map((o: any) => ({ id: o.id, name: o.name })));
} catch (e: any) {
setError(e?.message || "Failed to load cashup data");
} catch (e) {
setError(errorMessage(e) || "Failed to load cashup data");
} finally {
setLoading(false);
}
@@ -151,8 +152,8 @@ function CostsTab({ eventId, token, costs, eventOptions, isClosed, onChanged }:
}
setEditingId(null);
onChanged();
} catch (e: any) {
setErr(e?.message || "Failed to save cost");
} catch (e) {
setErr(errorMessage(e) || "Failed to save cost");
} finally {
setSaving(false);
}
@@ -163,8 +164,8 @@ function CostsTab({ eventId, token, costs, eventOptions, isClosed, onChanged }:
try {
await apiFetch(`/api/costs/${id}`, { method: "DELETE", authToken: token });
onChanged();
} catch (e: any) {
alert(e?.message || "Failed to delete cost");
} catch (e) {
alert(errorMessage(e) || "Failed to delete cost");
}
};
@@ -461,8 +462,8 @@ function PersonCashCountEditor({ eventId, token, userId, initialDenominations, i
method: "PUT", authToken: token, body: { denominations: lines, notes: notes || null }
});
onSaved();
} catch (e: any) {
setErr(e?.message || "Failed to save count");
} catch (e) {
setErr(errorMessage(e) || "Failed to save count");
} finally {
setSaving(false);
}
@@ -659,8 +660,8 @@ function ReconciliationTab({ eventId, token, data, busy, setBusy, setError, onCh
try {
await apiFetch(`/api/cashups/event/${eventId}/draft`, { method: "PUT", authToken: token, body: { lines: buildLinesPayload() } });
onChanged();
} catch (e: any) {
setError(e?.message || "Failed to save draft");
} catch (e) {
setError(errorMessage(e) || "Failed to save draft");
} finally {
setBusy(false);
}
@@ -673,8 +674,8 @@ function ReconciliationTab({ eventId, token, data, busy, setBusy, setError, onCh
await apiFetch(`/api/cashups/event/${eventId}/close`, { method: "POST", authToken: token, body: { lines: buildLinesPayload(), notes: closeNotes || null } });
onChanged();
onClosed();
} catch (e: any) {
setError(e?.message || "Failed to close event");
} catch (e) {
setError(errorMessage(e) || "Failed to close event");
} finally {
setBusy(false);
}
@@ -687,8 +688,8 @@ function ReconciliationTab({ eventId, token, data, busy, setBusy, setError, onCh
await apiFetch(`/api/cashups/event/${eventId}/close`, { method: "POST", authToken: token, body: { notes: closeNotes || null } });
onChanged();
onClosed();
} catch (e: any) {
setError(e?.message || "Failed to close event");
} catch (e) {
setError(errorMessage(e) || "Failed to close event");
} finally {
setBusy(false);
}
@@ -700,8 +701,8 @@ function ReconciliationTab({ eventId, token, data, busy, setBusy, setError, onCh
try {
await apiFetch(`/api/cashups/event/${eventId}/reopen`, { method: "POST", authToken: token, body: { notes: reopenNotes || null } });
onChanged();
} catch (e: any) {
setError(e?.message || "Failed to reopen event");
} catch (e) {
setError(errorMessage(e) || "Failed to reopen event");
} finally {
setBusy(false);
}
@@ -6,6 +6,7 @@ import { useAuth } from "@/hooks/useAuth";
import { apiFetch } from "@/lib/api";
import { useDismissingState } from "@/hooks/useDismissingState";
import { Wallet } from "lucide-react";
import { errorMessage } from "@/lib/utils";
export default function CashupLandingPage() {
const { token } = useAuth();
@@ -27,8 +28,8 @@ export default function CashupLandingPage() {
try {
const evs = await apiFetch<any[]>("/api/events/all?includePast=true&includeInactive=true", { authToken: token });
setEvents(Array.isArray(evs) ? evs : []);
} catch (e: any) {
setError(e?.message || "Failed to load events");
} catch (e) {
setError(errorMessage(e) || "Failed to load events");
} finally {
setLoading(false);
}
@@ -6,6 +6,7 @@ import { useRouter } from "next/navigation";
import { apiFetch } from "@/lib/api";
import { useDismissingState } from "@/hooks/useDismissingState";
import { ClipboardList } from "lucide-react";
import { errorMessage } from "@/lib/utils";
const STATUS_OPTIONS = ["pending", "confirmed", "partial_paid", "paid", "cancelled"] as const;
@@ -57,8 +58,8 @@ export default function AdminRegistrationsPage() {
return (a.user?.name || a.userId).localeCompare(b.user?.name || b.userId);
}) : [];
setRegistrations(list);
} catch (e: any) {
setError(e?.message || "Failed to load registrations");
} catch (e) {
setError(errorMessage(e) || "Failed to load registrations");
} finally {
setLoadingRegs(false);
}
@@ -117,8 +118,8 @@ export default function AdminRegistrationsPage() {
await apiFetch(`/api/registrations/${encodeURIComponent(reg.id)}`, { method: "DELETE", authToken: token });
setInfo("Registration cancelled");
await loadRegistrations();
} catch (e: any) {
setError(e?.message || "Failed to cancel registration");
} catch (e) {
setError(errorMessage(e) || "Failed to cancel registration");
}
};
@@ -129,8 +130,8 @@ export default function AdminRegistrationsPage() {
await apiFetch(`/api/registrations/${encodeURIComponent(reg.id)}`, { method: "PUT", authToken: token, body: { status } });
setInfo("Status updated");
await loadRegistrations();
} catch (e: any) {
setError(e?.message || "Failed to update status");
} catch (e) {
setError(errorMessage(e) || "Failed to update status");
}
};
@@ -3,7 +3,7 @@
import React, { Suspense, useCallback, useEffect, useRef, useState } from "react";
import { useAuth } from "@/hooks/useAuth";
import { useRouter, useSearchParams } from "next/navigation";
import { apiFetch, API_BASE, resolveToApiOrigin } from "@/lib/api";
import { apiFetch, ApiError, API_BASE, resolveToApiOrigin } from "@/lib/api";
import { useSiteSettings } from "@/contexts/SiteSettingsContext";
import { useDismissingState } from "@/hooks/useDismissingState";
import { Building2, Palette, Bell, Mail, Scale, MessageCircle, CreditCard, DatabaseBackup, type LucideIcon } from "lucide-react";
@@ -13,6 +13,7 @@ import { BackupsTab } from "@/components/admin/BackupsTab";
import { YocoTab } from "@/components/admin/YocoTab";
import { extractDominantColors } from "@/lib/extractColors";
import { mapsSearchUrl } from "@/lib/maps";
import { errorMessage } from "@/lib/utils";
type TabId = "organisation" | "branding" | "notifications" | "email" | "legal" | "whatsapp" | "yoco" | "backups";
@@ -194,8 +195,8 @@ function SiteSettingsPageInner() {
await apiFetch("/api/settings", { method: "PUT", authToken: token, body: updates });
setResult({ ok: true, message: "Saved." });
reloadSettings();
} catch (e: any) {
setResult({ ok: false, message: e?.message || "Failed to save" });
} catch (e) {
setResult({ ok: false, message: errorMessage(e) || "Failed to save" });
} finally {
setSaving(false);
}
@@ -253,8 +254,8 @@ function SiteSettingsPageInner() {
setSuggestedColors([]);
setResult({ ok: true, message: "Saved." });
reloadSettings();
} catch (e: any) {
setResult({ ok: false, message: e?.message || "Failed to save" });
} catch (e) {
setResult({ ok: false, message: errorMessage(e) || "Failed to save" });
} finally {
setSaving(false);
}
@@ -278,8 +279,8 @@ function SiteSettingsPageInner() {
setSuggestedColors([]);
setResult({ ok: true, message: "Reset to defaults." });
reloadSettings();
} catch (e: any) {
setResult({ ok: false, message: e?.message || "Failed to reset" });
} catch (e) {
setResult({ ok: false, message: errorMessage(e) || "Failed to reset" });
} finally {
setSaving(false);
}
@@ -312,8 +313,8 @@ function SiteSettingsPageInner() {
if (smtpPass.trim() !== "") { setSmtpPassSet(true); setSmtpPass(""); }
setResult({ ok: true, message: "Saved." });
reloadSettings();
} catch (e: any) {
setResult({ ok: false, message: e?.message || "Failed to save" });
} catch (e) {
setResult({ ok: false, message: errorMessage(e) || "Failed to save" });
} finally {
setSaving(false);
}
@@ -347,8 +348,9 @@ function SiteSettingsPageInner() {
},
});
setSmtpTestResult({ ok: true, message: "Connection successful — check your inbox for a test email." });
} catch (e: any) {
setSmtpTestResult({ ok: false, message: e?.message || "SMTP test failed", raw: e?.data?.raw });
} catch (e) {
const raw = e instanceof ApiError && typeof e.data?.raw === "string" ? e.data.raw : undefined;
setSmtpTestResult({ ok: false, message: errorMessage(e) || "SMTP test failed", raw });
} finally {
setSmtpTesting(false);
}
@@ -923,8 +925,8 @@ function WhatsAppTab({ active }: { active: boolean }) {
setActionMsg({ type: "ok", text: res?.message || `${action} successful.` });
await fetchStatus();
await fetchConfig();
} catch (e: any) {
let msg = e?.message || `${action} failed.`;
} catch (e) {
let msg = errorMessage(e) || `${action} failed.`;
try { msg = JSON.parse(msg)?.message || msg; } catch {}
await fetchConfig();
if (!msg.includes("SESSION_NOT_FOUND")) {
@@ -947,8 +949,8 @@ function WhatsAppTab({ active }: { active: boolean }) {
await apiFetch("/api/whatsapp/config", { method: "POST", authToken: token!, body: { token: inputToken.trim(), instanceId: "" } });
setInputToken("");
await fetchConfig();
} catch (e: any) {
setActionMsg({ type: "err", text: e?.message || "Failed to save token." });
} catch (e) {
setActionMsg({ type: "err", text: errorMessage(e) || "Failed to save token." });
} finally {
setSavingToken(false);
}
@@ -965,8 +967,8 @@ function WhatsAppTab({ active }: { active: boolean }) {
await apiFetch("/api/whatsapp/config", { method: "POST", authToken: token!, body: { token: "", instanceId: inputInstanceId.trim() } });
setInputInstanceId("");
await fetchConfig();
} catch (e: any) {
setActionMsg({ type: "err", text: e?.message || "Failed to save Instance ID." });
} catch (e) {
setActionMsg({ type: "err", text: errorMessage(e) || "Failed to save Instance ID." });
} finally {
setSavingInstance(false);
}
@@ -979,8 +981,8 @@ function WhatsAppTab({ active }: { active: boolean }) {
const res = await apiFetch<any>("/api/whatsapp/create-instance", { method: "POST", authToken: token! });
setActionMsg({ type: "ok", text: res?.message || "Instance created." });
await fetchConfig();
} catch (e: any) {
setActionMsg({ type: "err", text: e?.message || "Failed to create instance." });
} catch (e) {
setActionMsg({ type: "err", text: errorMessage(e) || "Failed to create instance." });
} finally {
setSavingInstance(false);
}
@@ -1224,8 +1226,8 @@ function WhatsAppTab({ active }: { active: boolean }) {
setInputToken("");
setInputInstanceId("");
await fetchConfig();
} catch (e: any) {
setActionMsg({ type: "err", text: e?.message || "Failed to update." });
} catch (e) {
setActionMsg({ type: "err", text: errorMessage(e) || "Failed to update." });
} finally {
setSavingToken(false);
}
@@ -8,6 +8,7 @@ import { useDismissingState } from "@/hooks/useDismissingState";
import { beginImpersonation } from "@/lib/impersonation";
import { Users as UsersIcon } from "lucide-react";
import { RoleBadge, type Role as RoleBadgeRole } from "@/components/shared/RoleBadge";
import { errorMessage } from "@/lib/utils";
interface UserItem {
id: string;
@@ -94,8 +95,8 @@ export default function AdminUsersPage() {
setTotal(res?.total ?? 0);
setTotalPages(res?.pages ?? 1);
setPage(p);
} catch (e: any) {
setError(e?.message || "Failed to load users");
} catch (e) {
setError(errorMessage(e) || "Failed to load users");
} finally {
setFetching(false);
}
@@ -132,8 +133,8 @@ export default function AdminUsersPage() {
resetCreateForm();
setCreateOpen(false);
await loadUsers(page);
} catch (e: any) {
setError(e?.message || "Failed to create user");
} catch (e) {
setError(errorMessage(e) || "Failed to create user");
} finally {
setCreating(false);
}
@@ -163,8 +164,8 @@ export default function AdminUsersPage() {
setEditingId(null);
setEditData({});
await loadUsers(page);
} catch (e: any) {
setError(e?.message || "Failed to save user");
} catch (e) {
setError(errorMessage(e) || "Failed to save user");
} finally {
setSaving(false);
}
@@ -174,8 +175,8 @@ export default function AdminUsersPage() {
if (!confirm(`Revoke all active sessions for ${name}? They will be signed out on all devices.`)) return;
try {
await apiFetch(`/api/users/${encodeURIComponent(id)}/revoke-sessions`, { method: "POST", authToken: token! });
} catch (e: any) {
setError(e?.message || "Failed to revoke sessions");
} catch (e) {
setError(errorMessage(e) || "Failed to revoke sessions");
}
};
@@ -190,8 +191,8 @@ export default function AdminUsersPage() {
try {
const res = await apiFetch<any>(`/api/admin/impersonate/${encodeURIComponent(impTarget.id)}`, { method: "POST", authToken: token, body: { reason: impReason } });
beginImpersonation(res);
} catch (e: any) {
setImpError(e?.message || "Couldn't start impersonation");
} catch (e) {
setImpError(errorMessage(e) || "Couldn't start impersonation");
setImpBusy(false);
}
};
@@ -206,8 +207,8 @@ export default function AdminUsersPage() {
try {
await apiFetch(`/api/users/${encodeURIComponent(id)}`, { method: "PUT", authToken: token!, body: { twoFactorEnabled: false } });
await loadUsers(page);
} catch (e: any) {
setError(e?.message || "Failed to reset two-factor authentication");
} catch (e) {
setError(errorMessage(e) || "Failed to reset two-factor authentication");
}
};
@@ -216,8 +217,8 @@ export default function AdminUsersPage() {
try {
await apiFetch(`/api/users/${encodeURIComponent(id)}`, { method: "DELETE", authToken: token! });
await loadUsers(page);
} catch (e: any) {
setError(e?.message || "Failed to deactivate user");
} catch (e) {
setError(errorMessage(e) || "Failed to deactivate user");
}
};
@@ -226,8 +227,8 @@ export default function AdminUsersPage() {
try {
await apiFetch(`/api/users/${encodeURIComponent(id)}/anonymize`, { method: "POST", authToken: token! });
await loadUsers(page);
} catch (e: any) {
setError(e?.message || "Failed to delete user data");
} catch (e) {
setError(errorMessage(e) || "Failed to delete user data");
}
};