feat(ui): error catalogue and describeError; dropped connections read as network copy

This commit is contained in:
sam committed 2026-11-13 13:33:39 +11:00
1 parent e389e1a21e
commit 0d9e2d1cd8
4 files changed
+66 -3

No files matched your search

+6
View File
@@ -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;
+8
View File
@@ -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;
+20 -3
View File
@@ -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<Response> {
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;