molecule-core/canvas/src/components/ErrorBoundary.tsx
Molecule AI Core-UIUX 6856b5a7cc
Some checks failed
CI / Canvas Deploy Reminder (pull_request) Blocked by required conditions
CI / all-required (pull_request) Blocked by required conditions
E2E Staging Canvas (Playwright) / Canvas tabs E2E (pull_request) Blocked by required conditions
Handlers Postgres Integration / Handlers Postgres Integration (pull_request) Blocked by required conditions
sop-checklist / all-items-acked (pull_request) [info tier:low] acked: 0/7 — missing: comprehensive-testing, local-postgres-e2e, staging-smoke, +4
Lint curl status-code capture / Scan workflows for curl status-capture pollution (pull_request) Successful in 28s
Harness Replays / detect-changes (pull_request) Successful in 35s
CI / Detect changes (pull_request) Successful in 2m3s
E2E API Smoke Test / detect-changes (pull_request) Successful in 2m5s
qa-review / approved (pull_request) Successful in 48s
security-review / approved (pull_request) Successful in 41s
lint-required-no-paths / lint-required-no-paths (pull_request) Successful in 1m45s
Lint workflow YAML (Gitea-1.22.6-hostile shapes) / Lint workflow YAML for Gitea-1.22.6-hostile shapes (pull_request) Failing after 1m36s
sop-checklist-gate / gate (pull_request) Successful in 38s
Runtime PR-Built Compatibility / detect-changes (pull_request) Successful in 1m30s
Lint pre-flip continue-on-error / Verify continue-on-error flips have run-log proof (pull_request) Successful in 3m10s
lint-continue-on-error-tracking / lint-continue-on-error-tracking (pull_request) Successful in 4m3s
CI / Shellcheck (E2E scripts) (pull_request) Successful in 3s
CI / Python Lint & Test (pull_request) Successful in 3s
Runtime PR-Built Compatibility / PR-built wheel + import smoke (pull_request) Successful in 9s
Block internal-flavored paths / Block forbidden paths (pull_request) Failing after 11m13s
E2E Staging Canvas (Playwright) / detect-changes (pull_request) Failing after 11m0s
Handlers Postgres Integration / detect-changes (pull_request) Failing after 10m57s
Harness Replays / Harness Replays (pull_request) Failing after 1m20s
E2E API Smoke Test / E2E API Smoke Test (pull_request) Successful in 1m57s
lint-required-context-exists-in-bp / lint-required-context-exists-in-bp (pull_request) Failing after 14m53s
Secret scan / Scan diff for credential-shaped strings (pull_request) Failing after 14m9s
gate-check-v3 / gate-check (pull_request) Failing after 14m2s
sop-tier-check / tier-check (pull_request) Failing after 13m20s
CI / Canvas (Next.js) (pull_request) Successful in 17m16s
CI / Platform (Go) (pull_request) Successful in 19m24s
fix(canvas): ErrorBoundary add role=alert aria-live=assertive
Error state was not announced to screen readers on crash. Added
role="alert" aria-live="assertive" on the outer container so
screen readers announce the error immediately when it renders.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-14 00:09:36 +00:00

109 lines
3.9 KiB
TypeScript

"use client";
import React from "react";
interface ErrorBoundaryProps {
children: React.ReactNode;
}
interface ErrorBoundaryState {
hasError: boolean;
error: Error | null;
}
export class ErrorBoundary extends React.Component<
ErrorBoundaryProps,
ErrorBoundaryState
> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void {
console.error("ErrorBoundary caught an error:", error, errorInfo.componentStack);
}
handleReload = () => {
window.location.reload();
};
handleReport = () => {
const errorDetails = {
message: this.state.error?.message ?? "Unknown error",
stack: this.state.error?.stack ?? "N/A",
timestamp: new Date().toISOString(),
url: window.location.href,
};
// Log the full report to console for collection by monitoring tools
console.error("Error Report:", JSON.stringify(errorDetails, null, 2));
// Copy error info to clipboard for manual reporting (button click is its
// own affordance — no native alert needed). On clipboard failure the
// console.error above still surfaces the report.
void navigator.clipboard?.writeText(JSON.stringify(errorDetails, null, 2))
.catch((e) => console.warn("clipboard write failed:", e));
};
render() {
if (this.state.hasError) {
return (
<div role="alert" aria-live="assertive" className="fixed inset-0 flex items-center justify-center bg-surface z-50">
<div className="max-w-md rounded-2xl border border-red-500/30 bg-surface-sunken/90 px-8 py-8 text-center shadow-2xl shadow-black/40">
<div className="mx-auto mb-4 flex h-14 w-14 items-center justify-center rounded-full bg-red-500/10 border border-red-500/30">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="#ef4444"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
<circle cx="12" cy="12" r="10" />
<line x1="12" y1="8" x2="12" y2="12" />
<line x1="12" y1="16" x2="12.01" y2="16" />
</svg>
</div>
<h2 className="text-lg font-semibold text-ink mb-2">
Something went wrong
</h2>
<p className="text-sm text-ink-mid mb-1">
An unexpected error occurred while rendering the application.
</p>
<p className="text-xs text-bad/80 mb-6 font-mono break-all">
{this.state.error?.message ?? "Unknown error"}
</p>
<div className="flex items-center justify-center gap-3">
<button
type="button"
onClick={this.handleReload}
className="rounded-lg bg-accent-strong hover:bg-accent px-5 py-2 text-sm font-medium text-white transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-surface"
>
Reload
</button>
<a
href="#report"
onClick={(e) => {
e.preventDefault();
this.handleReport();
}}
className="rounded-lg border border-line hover:border-line px-5 py-2 text-sm font-medium text-ink-mid hover:text-ink transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-surface"
>
Report
</a>
</div>
</div>
</div>
);
}
return this.props.children;
}
}