From 9e608dee5fc700abe00ae01c9713dd76b05ed6a7 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 6 Sep 2026 02:34:34 +1000 Subject: [PATCH] fix(webui): distinguish refused vs broken task actions, handle 401 (#753) Failed task actions in the dashboard rendered the same generic message regardless of cause. ApiError already carried the HTTP status, but nothing read it: a refused mutation (400/403/409 - bad input, CSRF, conflicting state) looked identical to a broken one (5xx, network drop, timeout), and an expired session (401, whether archied's own token cookie or an upstream forward-auth proxy) showed a generic failure instead of prompting re-authentication. Add classifyActionError() in api.jsx to map a caught error to a refused / broken / session-expired kind, and use it in tasks.jsx to render each with distinct framing and coloring (reusing the existing warn/danger tokens pill.css already establishes) - refused actions get "you can't do that", broken ones get "try again, or check the daemon", and an expired session gets a reload prompt. Refs archie-core-h3lg. --- ui/src/base/api.jsx | 16 ++++ ui/src/tasks/tasks.css | 19 ++++- ui/src/tasks/tasks.jsx | 29 ++++++- ui/test/classify-action-error.test.js | 28 +++++++ ui/test/tasks-actions.test.js | 106 ++++++++++++++++++++++++++ 5 files changed, 192 insertions(+), 6 deletions(-) create mode 100644 ui/test/classify-action-error.test.js diff --git a/ui/src/base/api.jsx b/ui/src/base/api.jsx index 5dbd5a75a..cda6c159d 100644 --- a/ui/src/base/api.jsx +++ b/ui/src/base/api.jsx @@ -51,6 +51,22 @@ export class ApiError extends Error { } } +// classifyActionError turns a failed operator action into a rendering +// decision. archied's own handlers already return distinguishable status +// codes for a refused mutation (400/403/409 -- bad input, missing CSRF, +// cross-origin, conflicting state) versus a broken one (5xx, or no status at +// all when fetch itself failed -- network drop, timeout); 401 additionally +// means the session -- archied's own token cookie, or an upstream +// forward-auth proxy sitting in front of it -- has expired rather than that +// the action was rejected or the daemon is unwell. +export function classifyActionError(err) { + const status = err instanceof ApiError ? err.status : undefined; + const message = err?.message || "Action failed"; + if (status === 401) return { kind: "session-expired", message }; + if (status >= 400 && status < 500) return { kind: "refused", message }; + return { kind: "broken", message }; +} + export const api = { summary: () => req("/api/summary"), tasks: () => req("/api/tasks"), diff --git a/ui/src/tasks/tasks.css b/ui/src/tasks/tasks.css index 6d0839a7b..a88292b69 100644 --- a/ui/src/tasks/tasks.css +++ b/ui/src/tasks/tasks.css @@ -96,12 +96,27 @@ } .task-action-error { - max-width: 180px; - color: var(--danger); + max-width: 220px; font-size: var(--text-xs); white-space: normal; } +.task-action-error-refused { + color: var(--warn); +} + +.task-action-error-broken { + color: var(--danger); +} + +.task-action-error-session { + color: var(--warn); + display: inline-flex; + align-items: center; + gap: var(--sp-1); + flex-wrap: wrap; +} + .task-decision { margin-bottom: var(--sp-4); padding: var(--sp-3); diff --git a/ui/src/tasks/tasks.jsx b/ui/src/tasks/tasks.jsx index cdd3a1b75..35d6f285a 100644 --- a/ui/src/tasks/tasks.jsx +++ b/ui/src/tasks/tasks.jsx @@ -1,7 +1,7 @@ import { Fragment } from "preact"; import { useState, useEffect, useRef } from "preact/hooks"; import "./tasks.css"; -import { api } from "../base/api.jsx"; +import { api, classifyActionError } from "../base/api.jsx"; import { ago } from "../base/dom.jsx"; import { Pill } from "../base/pill.jsx"; import { statTile as renderStatTile } from "../base/statTile.jsx"; @@ -20,6 +20,27 @@ function StatTileNode(props) { } +// renderActionError distinguishes a refused action ("you cannot do that" -- +// bad input, a conflicting task state) from a broken one (5xx, network drop, +// timeout -- "try again or check the daemon"), and special-cases an expired +// session with a prompt to reload rather than either framing. +function renderActionError({ kind, message }) { + if (kind === "session-expired") { + return ( + + Your session has expired.{" "} + + + ); + } + if (kind === "refused") { + return {message} — you can't do that.; + } + return {message} — try again, or check the daemon.; +} + function Empty({ title, detail }) { return (
@@ -113,7 +134,7 @@ function TasksApp({ query }) { next.delete(id); return next; }); - setActionErrors(prev => new Map(prev).set(id, err.message || "Action failed")); + setActionErrors(prev => new Map(prev).set(id, classifyActionError(err))); } }; @@ -190,8 +211,8 @@ function TasksApp({ query }) { }; const renderTaskActions = (t) => { - const message = actionErrors.get(t.id); - const errorMsg = message ? {message} : null; + const actionError = actionErrors.get(t.id); + const errorMsg = actionError ? renderActionError(actionError) : null; const busy = actionsInFlight.has(t.id); const variantToken = (kind) => diff --git a/ui/test/classify-action-error.test.js b/ui/test/classify-action-error.test.js new file mode 100644 index 000000000..8e888efd7 --- /dev/null +++ b/ui/test/classify-action-error.test.js @@ -0,0 +1,28 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import "./shim.js"; +import { ApiError, classifyActionError } from "../src/base/api.js"; + +test("classifyActionError treats 401 as an expired session", () => { + assert.equal(classifyActionError(new ApiError("unauthorised", 401)).kind, "session-expired"); +}); + +test("classifyActionError treats 400/403/409 as refused", () => { + assert.equal(classifyActionError(new ApiError("bad id", 400)).kind, "refused"); + assert.equal(classifyActionError(new ApiError("cross-origin mutation refused", 403)).kind, "refused"); + assert.equal(classifyActionError(new ApiError("task changed state; refresh and try again", 409)).kind, "refused"); +}); + +test("classifyActionError treats 5xx as broken", () => { + assert.equal(classifyActionError(new ApiError("task action failed", 500)).kind, "broken"); + assert.equal(classifyActionError(new ApiError("runtime task control is unavailable", 503)).kind, "broken"); +}); + +test("classifyActionError treats a statusless error (network/timeout) as broken", () => { + assert.equal(classifyActionError(new TypeError("Failed to fetch")).kind, "broken"); + assert.equal(classifyActionError(new DOMException("The operation was aborted", "AbortError")).kind, "broken"); +}); + +test("classifyActionError preserves the server's message", () => { + assert.equal(classifyActionError(new ApiError("cross-origin mutation refused", 403)).message, "cross-origin mutation refused"); +}); diff --git a/ui/test/tasks-actions.test.js b/ui/test/tasks-actions.test.js index a1e67c599..9139eaadf 100644 --- a/ui/test/tasks-actions.test.js +++ b/ui/test/tasks-actions.test.js @@ -62,6 +62,112 @@ test("an id NOT in the catalog renders nothing", async () => { assert.equal(root.querySelector(".task-actions"), null, "unknown action renders nothing"); }); +test("a refused action (403) renders 'you can't do that' framing, not a generic failure", async () => { + const task = { + id: 10, + title: "Refused task", + status: "waiting_human", + repo: "sam/archie", + workflow: "tdd", + actions: ["retry"], + }; + const root = await renderTask(task); + const { ApiError } = await import("../src/base/api.jsx"); + const original = api.taskAction; + api.taskAction = async () => { throw new ApiError("cross-origin mutation refused", 403); }; + try { + root.querySelector(".task-actions button").click(); + await waitFor(() => { + assert.ok(root.querySelector(".task-action-error-refused"), "a refused action should render the refused variant"); + }); + const el = root.querySelector(".task-action-error-refused"); + assert.match(el.textContent, /you can't do that/i); + assert.equal(root.querySelector(".task-action-error-broken"), null); + assert.equal(root.querySelector(".task-action-error-session"), null); + } finally { + api.taskAction = original; + } +}); + +test("a broken action (500) renders 'try again or check the daemon' framing", async () => { + const task = { + id: 11, + title: "Broken task", + status: "waiting_human", + repo: "sam/archie", + workflow: "tdd", + actions: ["retry"], + }; + const root = await renderTask(task); + const { ApiError } = await import("../src/base/api.jsx"); + const original = api.taskAction; + api.taskAction = async () => { throw new ApiError("task action failed", 500); }; + try { + root.querySelector(".task-actions button").click(); + await waitFor(() => { + assert.ok(root.querySelector(".task-action-error-broken"), "a broken action should render the broken variant"); + }); + const el = root.querySelector(".task-action-error-broken"); + assert.match(el.textContent, /try again, or check the daemon/i); + assert.equal(root.querySelector(".task-action-error-refused"), null); + assert.equal(root.querySelector(".task-action-error-session"), null); + } finally { + api.taskAction = original; + } +}); + +test("a network failure with no status renders the broken variant", async () => { + const task = { + id: 12, + title: "Network failure task", + status: "waiting_human", + repo: "sam/archie", + workflow: "tdd", + actions: ["retry"], + }; + const root = await renderTask(task); + const original = api.taskAction; + api.taskAction = async () => { throw new TypeError("Failed to fetch"); }; + try { + root.querySelector(".task-actions button").click(); + await waitFor(() => { + assert.ok(root.querySelector(".task-action-error-broken"), "a network failure should render the broken variant"); + }); + } finally { + api.taskAction = original; + } +}); + +test("a 401 action failure prompts re-authentication instead of a generic failure", async () => { + const task = { + id: 13, + title: "Expired session task", + status: "waiting_human", + repo: "sam/archie", + workflow: "tdd", + actions: ["retry"], + }; + const root = await renderTask(task); + const { ApiError } = await import("../src/base/api.jsx"); + const original = api.taskAction; + api.taskAction = async () => { throw new ApiError("unauthorised", 401); }; + try { + root.querySelector(".task-actions button").click(); + await waitFor(() => { + assert.ok(root.querySelector(".task-action-error-session"), "a 401 should render the session-expired variant"); + }); + const el = root.querySelector(".task-action-error-session"); + assert.match(el.textContent, /session has expired/i); + const reloadBtn = el.querySelector("button"); + assert.ok(reloadBtn, "the session-expired message should offer a reload action"); + assert.match(reloadBtn.textContent, /reload/i); + assert.equal(root.querySelector(".task-action-error-refused"), null); + assert.equal(root.querySelector(".task-action-error-broken"), null); + } finally { + api.taskAction = original; + } +}); + test("a link-kind action renders an anchor to the forge", async () => { const task = { id: 9,