Skip to content

Add individual exit codes for different error conditions#8129

Open
kovan wants to merge 2 commits intomitmproxy:mainfrom
kovan:exit-codes
Open

Add individual exit codes for different error conditions#8129
kovan wants to merge 2 commits intomitmproxy:mainfrom
kovan:exit-codes

Conversation

@kovan
Copy link
Copy Markdown

@kovan kovan commented Mar 15, 2026

Summary

  • Replace all sys.exit(1) calls with specific exit codes so parent processes can distinguish failure reasons
  • Add mitmproxy/utils/exit_codes.py defining grouped exit codes:
    • 10 — startup error (addon load failure, etc.)
    • 20 — no TTY (console requires interactive shell)
    • 30 — invalid arguments (unrecognized CLI flags)
    • 31 — invalid options (bad --set values)
    • 40 — cannot print (stdout broken)
    • 41 — cannot write to file (disk full, etc.)
  • Tests for all testable exit code paths, including INVALID_ARGS and INVALID_OPTIONS which stumped previous attempts

Picks up from #5133 and #5882.

Fixes #5696

Test plan

  • test_exit_codes.py — exit codes are distinct, SUCCESS is 0
  • test_errorcheck.py — startup errors exit with STARTUP_ERROR
  • test_termlog.py — print failure exits with CANNOT_PRINT
  • test_save.py — write failure exits with CANNOT_WRITE_TO_FILE
  • test_main.py — bad flags exit with INVALID_ARGS, bad options exit with INVALID_OPTIONS
  • All 23 tests pass
  • ruff clean

kovan and others added 2 commits March 15, 2026 12:56
Replace all sys.exit(1) calls with specific exit codes so that
parent processes can distinguish why mitmproxy exited.

Exit codes:
- 10: startup error (addon load failure, etc.)
- 20: no TTY (console requires interactive shell)
- 30: invalid arguments (unrecognized CLI flags)
- 31: invalid options (bad --set values)
- 40: cannot print (stdout broken)
- 41: cannot write to file (disk full, etc.)

Fixes mitmproxy#5696
@kovan
Copy link
Copy Markdown
Author

kovan commented Mar 23, 2026

Valid concern. Well-written shell scripts already check for non-zero (if cmd; then or || exit), but scripts that explicitly check == 1 would need updating. This is a backwards-incompatible change and should be called out in the changelog. I'll add a note in the PR description about the migration path.

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.

Add individual exit codes for different errors

1 participant