frontend: shared ApiErrorPanel component, promote readApiError from credentials.js
Closes #3410. `ApiErrorPanel` renders a ProblemDetails (RFC 9457) error nicely, with a copy button so the full text can be pasted straight into a bug report. No truncation of `detail` — on the #3363 incident that motivated this issue, that string was the entire diagnosis. `readApiError`/`problemMessage`/`ProblemDetails` are promoted out of credentials.js's original `readErrorBody` into `@hive/shared/api-error.js` (comment rewritten: the RFC 9457 rework has already landed everywhere except swarm-controller's status route, #3412 in flight, so the raw-text fallback is a compat shim for that one gap, not a general transition). credentials.js's 4 call sites switch to the shared reader (kept as one-line messages there, its result slots are single-line aria-live regions, not a panel context). Wired ApiErrorPanel into OverviewPage.tsx (the issue's own worked example) and CreateAgentPage.tsx (second real call site). Not built on <hive-warn> despite matching its visual language — that custom element's CSS-as-text import only works under a build with loader: 'text' for .css (dashboard's), and silently renders unstyled under swarm-ui's default css loader (filed separately as #3415). ApiErrorPanel is a self-contained light-DOM component instead, per mara's own suggestion to keep it independent of the old UI's shapes.
This commit is contained in:
parent
bfe921c254
commit
f60aab4717
8 changed files with 230 additions and 45 deletions
|
|
@ -0,0 +1,77 @@
|
|||
// ApiErrorPanel.tsx — <ApiErrorPanel>, the shared "show the operator why
|
||||
// an API call failed" component (mara, on the issue that asked for this:
|
||||
// "the error display component is for showing ProblemDetails in a nicer
|
||||
// way with copy button [...] wherever we want to show an error, this
|
||||
// component should be used"). Renders a `ProblemDetails` (./api-error.ts)
|
||||
// — heading + the full `detail` text, unclamped (it can be a raw NATS/
|
||||
// JetStream error, and on the incident that prompted this component
|
||||
// *that string was the entire diagnosis* — truncating it defeats the
|
||||
// point) — plus a copy button for pasting straight into a bug report.
|
||||
//
|
||||
// Deliberately NOT built on `<hive-warn>`, despite matching its visual
|
||||
// language, because that custom element's CSS-as-text import only
|
||||
// resolves under a build with `loader: 'text'` for `.css` (dashboard's);
|
||||
// swarm-ui's default `css` loader leaves its shadow `<style>` empty,
|
||||
// rendering unstyled with no build error (filed separately as a forge
|
||||
// issue against swarm-ui's build config). This component
|
||||
// is self-contained light-DOM instead, so it needs nothing beyond a
|
||||
// normal `.css` import either build already handles, matching
|
||||
// `JobqRollup`'s pattern. Colour semantics are still copied from
|
||||
// `hive-warn.css`'s `level="error"` rule on purpose — same look, no
|
||||
// shared shadow root.
|
||||
//
|
||||
// Same shared-component shape as `JobqRollup`/`JobqGraph`:
|
||||
// `render(h(ApiErrorPanel, { problem }), container)` from vanilla JS, or
|
||||
// `<ApiErrorPanel problem={...} />` from swarm-ui's JSX.
|
||||
import { useState } from 'preact/hooks';
|
||||
import type { ProblemDetails } from '../api-error.js';
|
||||
import './api-error-panel.css';
|
||||
|
||||
export interface ApiErrorPanelProps {
|
||||
problem: ProblemDetails;
|
||||
// Optional short prefix naming what failed ("failed to load the hive
|
||||
// roster") — the panel is otherwise just the server's own words, which
|
||||
// don't always say what the caller was trying to do.
|
||||
context?: string;
|
||||
}
|
||||
|
||||
function formatForCopy(p: ProblemDetails): string {
|
||||
const lines: string[] = [];
|
||||
if (p.status !== undefined) lines.push(`status: ${p.status}`);
|
||||
if (p.title) lines.push(`title: ${p.title}`);
|
||||
if (p.type) lines.push(`type: ${p.type}`);
|
||||
if (p.detail) lines.push(`detail: ${p.detail}`);
|
||||
return lines.length ? lines.join('\n') : 'request failed';
|
||||
}
|
||||
|
||||
export function ApiErrorPanel({ problem, context }: ApiErrorPanelProps) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
const heading = problem.title || (problem.status !== undefined ? `http ${problem.status}` : 'request failed');
|
||||
|
||||
async function copy() {
|
||||
try {
|
||||
await navigator.clipboard.writeText(formatForCopy(problem));
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 1500);
|
||||
} catch {
|
||||
// clipboard permission denied / unavailable (e.g. non-secure
|
||||
// context) — the text is still fully visible to select by hand,
|
||||
// so this is a silent no-op rather than a second error to show.
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div class="api-error-panel" role="alert">
|
||||
<div class="api-error-heading">
|
||||
<span class="api-error-title">
|
||||
{context ? `${context}: ` : ''}
|
||||
{heading}
|
||||
</span>
|
||||
<button type="button" class="api-error-copy" onClick={copy}>
|
||||
{copied ? 'copied' : 'copy'}
|
||||
</button>
|
||||
</div>
|
||||
{problem.detail ? <p class="api-error-detail">{problem.detail}</p> : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
/* api-error-panel.css — `<ApiErrorPanel>`'s own styling, colour + layout
|
||||
both. Not slotted into `<hive-warn>` (see the component's own header
|
||||
for why) — the border/background/pulse below are deliberately copied
|
||||
from `hive-warn.css`'s `level="error"` rule rather than invented fresh,
|
||||
so this still reads as "the same error banner" everywhere it appears,
|
||||
just declared locally instead of shared through a shadow root. Reuses
|
||||
theme.css's own documented semantics (`--red` = "errors, fail state"),
|
||||
same as `hive-warn.css` does. */
|
||||
|
||||
.api-error-panel {
|
||||
display: block;
|
||||
margin-top: 1em;
|
||||
margin-bottom: 0.6em;
|
||||
border: 1px solid var(--red);
|
||||
border-radius: 4px;
|
||||
padding: 0.5em 0.8em;
|
||||
color: var(--red);
|
||||
background: color-mix(in srgb, var(--red) 8%, transparent);
|
||||
text-shadow: 0 0 6px color-mix(in srgb, currentColor 40%, transparent);
|
||||
animation: api-error-panel-pulse 2.4s ease-in-out infinite;
|
||||
}
|
||||
@keyframes api-error-panel-pulse {
|
||||
0%, 100% { box-shadow: 0 0 12px -4px color-mix(in srgb, currentColor 55%, transparent); }
|
||||
50% { box-shadow: 0 0 22px -2px color-mix(in srgb, currentColor 95%, transparent); }
|
||||
}
|
||||
.api-error-heading {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
justify-content: space-between;
|
||||
gap: 0.8em;
|
||||
}
|
||||
.api-error-title {
|
||||
font-weight: 600;
|
||||
}
|
||||
.api-error-copy {
|
||||
flex: none;
|
||||
background: transparent;
|
||||
color: inherit;
|
||||
border: 1px solid currentColor;
|
||||
border-radius: 0.3em;
|
||||
padding: 0.1em 0.6em;
|
||||
font: inherit;
|
||||
font-size: 0.85em;
|
||||
cursor: pointer;
|
||||
}
|
||||
.api-error-copy:hover {
|
||||
background: color-mix(in srgb, currentColor 12%, transparent);
|
||||
}
|
||||
.api-error-detail {
|
||||
margin: 0.5em 0 0;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
font-size: 0.9em;
|
||||
opacity: 0.9;
|
||||
}
|
||||
Loading…
Reference in a new issue