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
+6 -2
View File
@@ -72,8 +72,12 @@ export function resolveToApiOrigin(urlOrPath: string | null | undefined): string
return API_BASE ? `${API_BASE}${raw}` : raw;
}
// The parsed JSON body of a failed request — the backend always sends `message`, and
// some routes add a machine-readable `code` or extra detail fields.
export type ApiErrorData = { message?: string; code?: string; [key: string]: unknown };
export class ApiError extends Error {
constructor(public readonly status: number, message: string, public readonly data?: any) {
constructor(public readonly status: number, message: string, public readonly data?: ApiErrorData) {
super(message);
this.name = "ApiError";
}
@@ -110,7 +114,7 @@ export async function apiFetch<T = any>(path: string, options: ApiOptions = {}):
if (!res.ok) {
const text = await res.text().catch(() => "");
let message = text || `Request failed with status ${res.status}`;
let data: any;
let data: ApiErrorData | undefined;
// Backend returns { message: "..." } — extract it so callers get a plain string
try {
const json = JSON.parse(text);
+8
View File
@@ -25,3 +25,11 @@ export function truncateText(text: string, maxLength: number): string {
if (text.length <= maxLength) return text
return `${text.slice(0, maxLength)}...`
}
// Message from a caught value (an Error, an ApiError, or anything thrown), or undefined when
// it has none — so callers can write `errorMessage(e) || "Fallback text"`.
export function errorMessage(e: unknown): string | undefined {
if (e instanceof Error) return e.message || undefined
if (typeof e === "object" && e !== null && "message" in e && typeof e.message === "string") return e.message || undefined
return undefined
}