Skip to content

Use predefined exception instead of arbitrary exit code in command modules#995

Open
zhenchaoni wants to merge 3 commits into
mainfrom
private/zhenni/consistent_exit_code
Open

Use predefined exception instead of arbitrary exit code in command modules#995
zhenchaoni wants to merge 3 commits into
mainfrom
private/zhenni/consistent_exit_code

Conversation

@zhenchaoni

Copy link
Copy Markdown
Member

Summary

Fixes #509. Replaces bare sys.exit(N) / ctx.exit(N) / raise SystemExit(N) calls in command modules with predefined, typed exceptions so exit codes are managed consistently instead of scattered as magic numbers. No exit-code definitions change — codes 1/2/3/4 are preserved exactly.

Changes

  • Added three typed exceptions in utils/cli.py:
    • ModelLoadError → exit 3 (model fails to load on device/EP)
    • InferenceError → exit 4 (prediction fails at runtime)
    • PartialSupportError → exit 1 (valid negative result; raised silently, no Error: prefix)
    • ModelLoadError/InferenceError override show() to print the message verbatim, preserving existing stderr output.
  • analyze.py: removed sys import; all exits now raise UsageError (2) or PartialSupportError (1); added a re-raise guard so intentional exits aren't relabeled "Analysis failed".
  • run.py: parse/format/file errors → UsageError (2); load → ModelLoadError (3); inference → InferenceError (4); success no-input path keeps ctx.exit(0).
  • perf.py / serve.py: install/load failures → ClickException (1), with a re-raise guard in perf.

Exit code contract (unchanged)

Code Meaning
0 success
1 negative result (partial support)
2 usage error
3 model load failure
4 inference failure

Testing

  • Existing run tests asserting exit 3/4 are unchanged and still pass — behavior is fully guarded.
  • 261 unit tests pass (run, run_spec, perf_cli, optracing, serve); no type/lint errors.

@zhenchaoni zhenchaoni requested a review from a team as a code owner June 29, 2026 07:18
@zhenchaoni zhenchaoni changed the title Private/zhenni/consistent exit code Use predefined exception instead of arbitrary exit code in command modules Jun 29, 2026
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.

[winml] [P1] Bare sys.exit(N) calls in command modules; no documented exit-code contract

1 participant