Skip to content

P1B: Refactor (packages/opencode/src/cli/error.ts:35): Function with high complexity (count = 37): FormatError - #75

Open
haq2026 wants to merge 3 commits into
CMU-17313Q:mainfrom
haq2026:refactor-format-error-dispatch
Open

P1B: Refactor (packages/opencode/src/cli/error.ts:35): Function with high complexity (count = 37): FormatError#75
haq2026 wants to merge 3 commits into
CMU-17313Q:mainfrom
haq2026:refactor-format-error-dispatch

Conversation

@haq2026

@haq2026 haq2026 commented Sep 5, 2026

Copy link
Copy Markdown

1. Issue

Link to the associated GitHub issue:
Closes #74

Full path to the refactored file:
packages/opencode/src/cli/error.ts

What do you think this file does?
It converts internal error objects into human-readable messages for the CLI. FormatError recognizes known error types (config errors, provider errors, account errors, CLI errors, UI cancellation) and returns a formatted string for each; FormatUnknownError is the fallback for anything unrecognized.

What is the scope of your refactoring within that file?
Only FormatError and the new helper functions extracted from it. FormatUnknownError and the existing private helpers (isTaggedError, configData, stringField, configIssues) are unchanged.

2. Code smell

Which code smell did you fix?
Qlty flagged FormatError at line 35 for two smells: high complexity (count = 37) and many returns (count = 13).

Qlty output before:

packages/opencode/src/cli/error.ts
  35  Function with many returns (count = 13): FormatError
  35  Function with high complexity (count = 37): FormatError

Qlty output after: no smells reported for this file.

What did you change?
Each error-type branch was extracted into its own named formatter function (formatCliError, formatMcpFailed, formatAccountError, formatProviderModelNotFound, formatProviderInit, formatConfigJson, formatConfigDirectoryTypo, formatConfigFrontmatter, formatConfigRemoteAuth, formatConfigInvalid, formatUICancelled). FormatError now iterates an ordered FORMATTERS array and returns the first non-undefined result. The array preserves the original branch order, so precedence is unchanged, and the process.exitCode side effect stays inside the CliError branch exactly as before.

3. Testing

How did you verify your change?
packages/opencode/test/cli/error.test.ts was extended from 6 tests to 13. The existing 6 already covered the config, provider model, provider init and UI cancellation branches; the 7 new tests cover the previously untested paths: CliError with and without a message (including asserting process.exitCode is set to the error's exit code), remote config auth errors with and without a URL, config invalid with no path and no issues, the cause.body unwrapping path, and the fallback returning undefined for unrecognized input.

Which tests cover the refactored code, and why are they sufficient?
All 13 tests in packages/opencode/test/cli/error.test.ts exercise FormatError directly. Line coverage on src/cli/error.ts is 106 of 108 lines (98.1%); the only two uncovered lines are the MCPFailed branch, which was untested before this change as well and is unmodified by it. Because this refactor is strictly behavior-preserving, every branch that changed shape is asserted against the exact same output string it produced before, so any drift in message text, branch ordering, or the exit-code side effect would fail these tests. They pass.

Test results:

  • bun test test/cli/error.test.ts gives 13 pass, 0 fail
  • Full suite: 3264 pass, 1 fail. The single failure is chunkTimeout raises a response stream error when SSE body stalls, a network-timeout test that was already failing on main before this branch and is unrelated to this change.
  • bun lint gives 698 warnings and 2 errors, identical to the pre-existing count on main. No new lint issues introduced.

Note on the diff: this branch also contains the .devcontainer/devcontainer.json commit from P1A, which lives on my fork's main and has not been merged upstream. It is unrelated to this refactor.

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.

P1B: Refactor (packages/opencode/src/cli/error.ts:35): Function with high complexity (count = 37): FormatError

1 participant