Skip to content

refactor(cli): exceptions carry their codes as class data, and one exit seam replaces 19 hand-rolled exits #1006

Closed
2 of 2 issues completed

Description

@dan-petty

Context & Rationale

src/devops_cli/exceptions/ is 88 classes and 2,426 lines whose interface equals its implementation. Checked at 9c879ae.

  • Every class has an __init__, but 87 of 88 only forward keyword arguments to the base; only DevOpsCLIError (base.py, 56 lines) has behaviour (secret masking, __str__, to_dict).
  • 32 classes are never raised in src, 57 are never caught by name, 35 are raised from exactly one module; 47 appear in pytest.raises.
  • Error codes come three ways: 36 via CONST_ERROR_CODE_* (42 constants whose only importer is this package), 14 inline literals, 5 DEFAULT_ERROR_CODE class attributes, 3 of which duplicate the literal passed to super().
  • The exit seam (except DevOpsCLIError → typer.Exit(exc.exit_code)) lives in core/command_decorator.py::cli_command_handler, which no command module imports (docs: hand-written docs name only commands, options and components that exist #921). 19 hand-rolled raise typer.Exit(code=exc.exit_code) sites exist, 9 of them in commands/cloudflare.py.

Deletion test: delete the 87 __init__s and nothing reappears, callers already pass the kwargs the base accepts; delete the 32 never-raised classes and nothing reappears.

Scope & Deliverables

  • Deliverable: error_code and exit_code are class attributes read by the base __init__; domain bases (KubernetesError, VaultError, ...) add only their structured-detail keys; never-raised classes go; one exit seam applied by new_typer replaces the 19 hand-rolled sites; the CONST_ERROR_CODE_* constants come home as class data (they are identities, not matching lists).
  • Tests: test_exceptions.py::test_one_error_code_maps_to_one_exit_status becomes a table check; the 47 pytest.raises sites are unchanged; test_domain_specific_exceptions_exist shrinks with the deleted classes.
  • Key questions (open):
    • Does cli_command_handler become the seam every command passes through, or does new_typer carry it?
    • One sentence for AGENTS.md: exit and error codes live on the exception class.
  • Constraint: feat(cli): blind exception handling policy #423 (blind exception handling) is about except sites, not the taxonomy; the two do not overlap.

Strategic Alignment

Scope added 2026-10-02: checked at 38517a8. A second exit seam exists: #945 (6e5bb1f) added exit_on_error (core/cli.py:187-200), which prints the error without a traceback and exits with its exit_code. It is used 8 times (commands/rag.py 5, commands/ai_ingest.py 3). The hand-rolled raise typer.Exit(... exc.exit_code) sites are 20, not 19: commands/cloudflare.py 9, commands/valkey.py 9, commands/sandbox.py:979 and commands/telemetry.py:523. Five more typer.Exit(<result>.exit_code) lines pass a subprocess or verdict result and are not exception sites. Every command except the config group passes through OTelTyper.command, which wraps it in _execute_traced_cli_command (core/cli.py:76-106, :159-172). commands/config.py:45 builds a plain typer.Typer. AGENTS.md:393 names CONST_ERROR_CODE_*, and so does docs/SDLC.md:130-132. ai/instruction_generator.py has no counterpart, and AGENTS.md is curated, not generated (#840). ARCHITECTURE.md:167-172 describes @cli_command_handler on every command; #921 (v0.2.28) rewrites that text to describe new_typer().

Acceptance criteria

  • DevOpsCLIError reads error_code and exit_code from class attributes. The __init__ methods that only forward keyword arguments are deleted, and domain bases add only their structured-detail keys.
  • Classes never raised in src are deleted. The PR lists them with the search that shows no raise.
  • The CONST_ERROR_CODE_* constants (40 in config/constants.py at 38517a8, not 42) become class data, and config/constants.py keeps none of them.
  • The wrapper in OTelTyper.command is the one exit seam. It reports a DevOpsCLIError with print_error(exc.message, safe=True) and exits with exc.exit_code. cli_command_handler (core/command_decorator.py), exit_on_error, the 20 hand-rolled sites and the 8 exit_on_error blocks are deleted.
  • A test registers a command on new_typer() that raises a DevOpsCLIError subclass whose message holds Rich markup. It asserts the exit code, one error line, escaped markup and no traceback.
  • tests/test_exceptions.py::test_one_error_code_maps_to_one_exit_status checks a table built from the class hierarchy.
  • commands/config.py builds its app with new_typer(), so every command passes through the seam. A test walks the registered apps and finds no plain typer.Typer.
  • AGENTS.md:393 and docs/SDLC.md:130-132 are edited in place to say that an exception class declares its error_code and exit_code. AGENTS.md is not regenerated (docs(agents): instruction surfaces state only what is true, and devops ai agents never overwrites AGENTS.md #840). ARCHITECTURE.md names OTelTyper.command as the exit seam and no longer names cli_command_handler.
  • docs/ERRORS.md is regenerated (docs/generator.py:1051) and lists the same codes and exit statuses as before.
  • The new and changed tests run offline, with no model, network or cluster.
  • uv run devops ci passes.
  • (person-run) devops cloudflare and devops rag commands that fail on a missing token print one error line and exit with the same code as before.

Key questions

Update 2026-10-08: refinement

OTelTyper has exit_on (#767, core/cli.py:118-120), used by commands/gh.py and commands/release.py; this item makes it the default. Counts: 20 hand-rolled exits, 13 exit_on_error blocks, 103 classes, 44 codes.

  • new_typer() reports every DevOpsCLIError by default; no app passes exit_on=.
  • exit_on_error is only that wrapper's implementation; cli_command_handler, the 20 sites and 13 blocks are deleted.

Sub-issues

Sub-issues

2 of 2 Issues completed
Loading 2 sub-issues

Activity

  1. added
    type/refactor
    scope/cli
    priority/p3-low
    on Oct 2, 2026
  2. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Two more hand-rolled exits for the seam, checked at 910b823. Neither is among the 20 typer.Exit(... exc.exit_code) sites counted here, because both exit 1 instead of exc.exit_code: roadmap run catches RoadmapRunError, prints it and raises typer.Exit(1) (commands/roadmap.py:554-556), and commands/roadmap.py has _exit_on_failure (:101-107, 13 uses), which prints ': ' with safe=True and exits 1. #1365 changes :554-556 to print the RoadmapRunError message with safe=True (the message will name each failed job and its error). It leaves CLI traceback policy to this item.

    (This item's #983 note is part of the single combined #983 comment in graphql-budget's related_edits.)

  3. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Linked: #1372. The pr app has no exit_on (commands/pr.py:61-64), so a GitHubOperationError escapes as a traceback; this item's default exit path covers it and removes any exit_on= #1372 adds. Found by the 2026-10-08 backlog review.

  4. added
    status/ready
    value/low
    effort/medium
    on Oct 28, 2026
  5. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    This item is too large for one pull request, so it becomes two sub-issues. Each has its own change and pull request.

    Evidence (origin/main 08d70ca, 2026-10-28):

    • cli_command_handler is already gone. Only CHANGELOG.md names it.
    • The exit side: 14 apps pass exit_on=, 40 exit_on_error blocks are in 15 command modules, and 21 sites raise typer.Exit(exc.exit_code) by hand (cloudflare.py 9, valkey.py 9, sandbox.py, telemetry.py, scan.py). commands/config.py builds a plain typer.Typer.
    • The class side: 96 classes, 32 __init__ methods that only call super().__init__, and 46 CONST_ERROR_CODE_* constants.
    • The two sides change different modules and can merge in either order. Together they change about 35 modules.

    Decisions on the key questions:

    • The seam is the wrapper in OTelTyper.command, which new_typer() gives every command. A decorator can be forgotten; the wrapper cannot. An app callback with invoke_without_command=True gets the same report.
    • roadmap's _exit_on_failure exits with status 1, not with the error's exit_code. It stays as it is, because a change to it changes exit statuses. A later item can move it.
  6. dan-petty commented on Oct 29, 2026

    @dan-petty
    OwnerAuthor

    Both parts of this item are merged:

  7. removed
    status/ready
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p3-lowscope/clitype/refactorvalue/low

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions