Skip to content

refactor(errors): unify error output as YAML envelope to stderr#923

Merged
jackwener merged 5 commits intomainfrom
feat/error-envelope
Apr 9, 2026
Merged

refactor(errors): unify error output as YAML envelope to stderr#923
jackwener merged 5 commits intomainfrom
feat/error-envelope

Conversation

@jackwener
Copy link
Copy Markdown
Owner

Summary

  • Replace 100+ line chalk renderError() switch-case with unified YAML envelope output to stderr
  • Add toEnvelope() utility and ErrorEnvelope type to errors.ts
  • Remove ERROR_ICONS map, classifyGenericError regex classifier, and BrowserConnectError-specific bridge status display from commanderAdapter.ts
  • All errors now output structured {ok: false, error: {code, message, help, exitCode}} YAML to stderr regardless of TTY

Simplifies the error system from 5 mechanisms to 3:

  1. Error Envelope (YAML → stderr) — unified error output for all consumers
  2. Exit codes (sysexits.h) — process exit semantics
  3. Diagnostic (OPENCLI_DIAGNOSTIC=1) — autofix repair context

Net: -80 lines (107 added, 187 removed). Error output is now machine-readable for AI agents and scripts while remaining human-readable via YAML.

Example output

ok: false
error:
  code: AUTH_REQUIRED
  message: Not logged in to bilibili.com
  help: Please open Chrome or Chromium and log in to https://bilibili.com
  exitCode: 77

Test plan

  • errors.test.ts — 4 new tests for toEnvelope() (CliError, no-hint, unknown Error, non-Error)
  • commanderAdapter.test.ts — Updated error rendering tests to verify YAML envelope on stderr
  • Full test suite: 200/201 files pass (1 pre-existing e2e flake in tieba)
  • Build succeeds

Replace the 100+ line chalk renderError() switch-case with a single
YAML envelope output path. All errors now output a structured
{ok, error: {code, message, help, exitCode}} envelope to stderr,
regardless of TTY status.

This simplifies the error system from 5 mechanisms to 3:
1. Error Envelope (YAML → stderr) — unified error output
2. Exit codes (sysexits.h) — process exit semantics
3. Diagnostic (OPENCLI_DIAGNOSTIC=1) — autofix repair context

Removed: chalk error rendering, ERROR_ICONS map, classifyGenericError
regex classifier, BrowserConnectError-specific bridge status display.
Added: toEnvelope() utility, ErrorEnvelope type.
- Migrate xueqiu adapters from return [{error,help}] to throw CliError
- xueqiu/utils.ts: fetchXueqiuJson now throws AuthRequiredError/
  CommandExecutionError instead of returning {error, help} objects
- Remove resolveColumns error fallback from output.ts (no longer needed)
- Add verbose stack trace support to error envelope
- Add ADAPTER_LOAD to AutoFix hint trigger codes
- Update skill docs (adapter-templates, explorer, oneshot, advanced-patterns)
  to recommend throw CliError pattern instead of return [{error, help}]
…view fixes

- Remove `if ('error' in d) return [d]` from feed, hot, search, kline
  (fetchXueqiuJson now throws, so these were dead code)
- Add `stack?: string` to ErrorEnvelope interface (removes type cast hack)
- Fix adapter-templates.md: use AuthRequiredError instead of plain Error
Last two adapters that silently returned [] on error instead of
throwing CommandExecutionError.
…nsistency

- adapter-templates.md: getServerContext was throwing AuthRequiredError
  inside a function serialized into page.evaluate() (browser has no
  CliError). Reverted to return {error} sentinel + func() body throw.
- yahoo-finance/quote, barchart/quote: include symbol in fallback error msg
- xueqiu/kline: throw EmptyResultError instead of returning [] for
  consistency with other xueqiu adapters
@jackwener jackwener merged commit dbac7fc into main Apr 9, 2026
11 checks passed
@jackwener jackwener deleted the feat/error-envelope branch April 10, 2026 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant