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.
This commit is contained in:
sam authored and GitHub committed 2026-09-06 02:34:34 +10:00
1 parent 268283eb16
commit 9e608dee5f
5 files changed
+192 -6

No files matched your search

+16
View File
@@ -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"),
+17 -2
View File
@@ -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);
+25 -4
View File
@@ -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 (
<span className="task-action-error task-action-error-session">
Your session has expired.{" "}
<button className="button btn btn-small" type="button" onClick={() => window.location.reload()}>
Reload to sign in
</button>
</span>
);
}
if (kind === "refused") {
return <span className="task-action-error task-action-error-refused">{message} — you can't do that.</span>;
}
return <span className="task-action-error task-action-error-broken">{message} — try again, or check the daemon.</span>;
}
function Empty({ title, detail }) {
return (
<div className="empty">
@@ -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 ? <span className="task-action-error">{message}</span> : null;
const actionError = actionErrors.get(t.id);
const errorMsg = actionError ? renderActionError(actionError) : null;
const busy = actionsInFlight.has(t.id);
const variantToken = (kind) =>
+28
View File
@@ -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");
});
+106
View File
@@ -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,