diff --git a/ui/src/copy/errors/common.ts b/ui/src/copy/errors/common.ts new file mode 100644 index 000000000..53cecea22 --- /dev/null +++ b/ui/src/copy/errors/common.ts @@ -0,0 +1,6 @@ +export const common = { + sessionExpired: "Your session expired. Sign in again.", + network: "Archie can't be reached. Check your connection and try again.", + actionFailed: "Action failed", + unknown: "Something went wrong.", +} as const; diff --git a/ui/src/copy/errors/index.ts b/ui/src/copy/errors/index.ts new file mode 100644 index 000000000..7e016b24e --- /dev/null +++ b/ui/src/copy/errors/index.ts @@ -0,0 +1,8 @@ +// Every failure message the dashboard writes itself, by feature. Call sites +// dot-walk it, e.g. errors.chat.composer.microphoneUnavailable, so the copy is +// changed here and nowhere else. +import { common } from "./common.ts"; + +export const errors = { + common, +} as const; diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.ts index 0bde0ba3f..edc2ccb5e 100644 --- a/ui/src/lib/api.ts +++ b/ui/src/lib/api.ts @@ -4,6 +4,7 @@ // this one file. import { streamStateFor, type StreamState } from "./stream-state.ts"; import { streamURL } from "./stream-url.ts"; +import { errors } from "../copy/errors/index.ts"; // DEFAULT_TIMEOUT_MS bounds a request so a daemon that accepts the connection // but never answers cannot leave the UI in a loading state with no way back. @@ -95,20 +96,36 @@ export function classifyActionError(err: unknown): { message: string; } { const status = err instanceof ApiError ? err.status : undefined; - const message = - err instanceof Error && err.message ? err.message : "Action failed"; + const message = describeError(err, errors.common.actionFailed); if (status === 401) return { kind: "session-expired", message }; if (status !== undefined && status >= 400 && status < 500) return { kind: "refused", message }; return { kind: "broken", message }; } +// describeError is the only place a caught error becomes text. The server's +// own reason wins because it is what the operator can act on; anything without +// a message falls back to the caller's catalogue entry, never to String(err). +export function describeError(err: unknown, fallback: string): string { + if (err instanceof ApiError && err.status === 401) + return errors.common.sessionExpired; + if (err instanceof Error && err.message) return err.message; + return fallback; +} + // send is the only fetch() in the dashboard. It converts a non-ok response // into an ApiError carrying the server's own explanation and a usable status, // and hands the raw Response back so the streaming caller can read the body // itself. async function send(path: string, init: RequestInit): Promise { - const res = await fetch(path, init); + let res: Response; + try { + res = await fetch(path, init); + } catch (cause) { + // A caller's own abort (stopping a chat turn) is not a failure to report. + if (cause instanceof Error && cause.name === "AbortError") throw cause; + throw new ApiError(errors.common.network, 0); + } authenticationStateHandler?.(res.status === 401); if (!res.ok) throw new ApiError(await errorMessage(res), res.status); return res; diff --git a/ui/test/describe-error.test.ts b/ui/test/describe-error.test.ts new file mode 100644 index 000000000..acdbafbb6 --- /dev/null +++ b/ui/test/describe-error.test.ts @@ -0,0 +1,32 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { api, ApiError, describeError } from "../src/lib/api.ts"; +import { errors } from "../src/copy/errors/index.ts"; + +// Guards the one place a caught error becomes text: a dropped connection must +// read as the catalogue's network copy (not "TypeError: Failed to fetch"), an +// expired session as sign-in copy, and a thrown non-Error never as +// "[object Object]". +test("describeError turns a failure into operator copy", async () => { + const fallback = "Could not load tasks"; + const realFetch = globalThis.fetch; + globalThis.fetch = () => Promise.reject(new TypeError("Failed to fetch")); + let dropped: unknown; + try { + await api.tasks(); + } catch (err) { + dropped = err; + } finally { + globalThis.fetch = realFetch; + } + + const cases: { name: string; err: unknown; want: string }[] = [ + { name: "dropped connection", err: dropped, want: errors.common.network }, + { name: "expired session", err: new ApiError("unauthorized", 401), want: errors.common.sessionExpired }, + { name: "server reason", err: new ApiError("task is not awaiting approval", 409), want: "task is not awaiting approval" }, + { name: "plain object", err: { code: 1 }, want: fallback }, + { name: "empty message", err: new Error(""), want: fallback }, + ]; + for (const c of cases) assert.equal(describeError(c.err, fallback), c.want, c.name); +});