chain-studio/lib/data/error_presentation.dart
flemming-it 8f5cd2528b
Some checks failed
Security / Security check (push) Failing after 1s
feat(studio): central error helpers + inline log viewer + Today CTAs
Bundles the error-UX overhaul and the inline log viewer:

  - New `FaiLog` (`~/.fai/logs/studio-errors.log`, 256 KiB rotation,
    JSON-per-line). Every operator-visible failure is appended so
    `fai admin doctor` and the new viewer can show the trail
    without the operator having to reproduce the failure.
  - New `showFaiErrorSnack` / `showFaiErrorDialog` helpers wrap
    `FaiErrorBox` in copyable surfaces; 27 ad-hoc
    `SnackBar(content: Text(e.toString()))` sites swept to use
    them (settings, doctor, audit, store, module sheet, system-AI
    editor, flow output).
  - New `FaiLogViewer` modal (`showFaiLogViewer`) renders log
    files inline with line numbers, JSON-key + `[level]` token
    colouring, Copy-all, Refresh, Open-externally. Doctor's
    daemon-paths panel grows a "View" button next to "Open" for
    every `.log` row and now also lists the Studio errors log.
  - Today carousel: CTAs now actually re-run search after a
    `filterCategory` / `runQuery` story is tapped (was only
    flipping the chip state). Fallback story list bumped to 8.
  - Editor bumped to git ref carrying 0.15.0 (type-token
    colouring + analyzer diagnostics).

Studio bumped to 0.62.0.

Signed-off-by: flemming-it <stefan.a.flemming@googlemail.com>
2026-06-04 02:08:15 +02:00

114 lines
3.7 KiB
Dart

// Single entry point for surfacing an error to the operator.
// Every error pathway Studio shows (SnackBar, dialog, inline
// strip) should go through one of these helpers so the
// operator gets:
//
// • a copyable, selectable rendition of the message
// • a friendly headline (gRPC codes mapped via
// friendlyError) instead of a wall-of-stack-trace
// • an automatic append to ~/.fai/logs/studio-errors.log so
// the trail survives the dismiss action
//
// Call sites used to scatter `ScaffoldMessenger.of(ctx).showSnackBar(
// SnackBar(content: Text(e.toString())))` — that violated the
// "errors must be copyable" rule everywhere. These helpers
// replace those open-coded sites.
import 'package:flutter/material.dart';
import '../widgets/fai_error_box.dart';
import '../theme/tokens.dart';
import 'fai_log.dart';
/// Show [error] as a SnackBar with a copy affordance.
///
/// [source] is a short tag (`flows.run`, `audit.clear`, …)
/// used to group entries in the on-disk log so an operator or
/// `fai doctor` can scan recent failures by area without
/// reading the full free-text. Keep it stable; treat it like
/// a metric label, not free-form text.
///
/// [context] is the build-context the snack lands on. Falls
/// back to a Material default duration of 6 seconds — twice
/// the framework default so the operator has time to click
/// copy on an unfamiliar message.
void showFaiErrorSnack(
BuildContext context,
String source,
Object error, {
String? title,
Duration duration = const Duration(seconds: 6),
}) {
// Fire-and-forget log write. If the disk's full or the dir
// is read-only the UI still surfaces the error.
FaiLog.instance.error(source, error, context: title);
final messenger = ScaffoldMessenger.maybeOf(context);
if (messenger == null) {
// No Scaffold above — the caller is likely showing the
// error from inside a dialog. We've already logged; bail
// rather than throw.
return;
}
messenger.hideCurrentSnackBar();
final theme = Theme.of(context);
messenger.showSnackBar(
SnackBar(
duration: duration,
behavior: SnackBarBehavior.floating,
// Default SnackBar padding crushes our error box; reset
// to zero and let the box's own padding take over.
padding: EdgeInsets.zero,
content: Padding(
padding: const EdgeInsets.symmetric(
horizontal: FaiSpace.sm,
vertical: FaiSpace.xs,
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (title != null) ...[
Text(
title,
style: theme.textTheme.titleSmall?.copyWith(
color: theme.colorScheme.error,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: FaiSpace.xs),
],
FaiErrorBox(error: error, isError: true),
],
),
),
),
);
}
/// Show [error] as a centered modal dialog. Use this when the
/// failure blocks meaningful interaction (auth missing, hub
/// unreachable) — a SnackBar would be too easy to dismiss.
Future<void> showFaiErrorDialog(
BuildContext context,
String source,
Object error, {
String? title,
}) async {
FaiLog.instance.error(source, error);
return showDialog<void>(
context: context,
builder: (ctx) => AlertDialog(
title: title == null ? null : Text(title),
content: SizedBox(
width: 480,
child: FaiErrorBox(error: error, isError: true, maxHeight: 240),
),
actions: [
TextButton(
onPressed: () => Navigator.of(ctx).maybePop(),
child: const Text('OK'),
),
],
),
);
}