Skip to content

fix: handle malformed config files (ParsingError) + comprehensive tests#68

Open
ProjectNobi wants to merge 1 commit intochutesai:mainfrom
ProjectNobi:fix/config-error-handling-v2
Open

fix: handle malformed config files (ParsingError) + comprehensive tests#68
ProjectNobi wants to merge 1 commit intochutesai:mainfrom
ProjectNobi:fix/config-error-handling-v2

Conversation

@ProjectNobi
Copy link
Copy Markdown

Context

Follows up on closed #65 — addresses all feedback from @jondurbin:

"Incomplete, missing NoOptionError, also doesn't handle uninitialized config/auth config."

Since #65, commit 959252d already fixed NoOptionError and uninitialized config. This PR adds the one remaining gap: malformed config files that raise ParsingError (e.g. truncated files, missing section headers).

Changes

chutes/config.py (10 lines)

  • Wrap raw_config.read(CONFIG_PATH) in try/except for ParsingError
  • Follows the existing pattern: if not ALLOW_MISSING: raise NotConfigured(...)
  • Import ParsingError from configparser

tests/test_config.py (275 lines, 12 tests)

  • Comprehensive test suite covering all get_config() edge cases:
    • Missing config file (with/without ALLOW_MISSING)
    • Empty config file
    • Partial [auth] section (NoOptionError)
    • Empty [auth] section
    • Malformed config (no section headers) ← the new fix
    • Valid configs with/without [api] section
    • Env var overrides
    • ALLOW_MISSING interactions

All 12 tests pass.


T68Bot — Project Nobi

Addresses feedback from closed PR chutesai#65 and builds on Jon's fix in 959252d.

Jon's commit (959252d) already handled:
- NoOptionError missing from the auth-section try/except
- Unguarded raw_config.get('api', 'base_url') call

This PR adds one remaining gap + full test coverage:

1. raw_config.read(CONFIG_PATH) was unguarded against configparser.ParsingError
   (which includes MissingSectionHeaderError for files with no [section] headers).
   A malformed config.ini now raises NotConfigured with a clear message, or is
   silently skipped when CHUTES_ALLOW_MISSING=true — consistent with the rest of
   the error handling pattern.

2. tests/test_config.py — 12 unit tests covering every failure path:
   - Config file missing entirely
   - Config file empty
   - [auth] section absent
   - [auth] section present but options missing (NoOptionError)
   - [auth] section header only, no keys
   - Malformed file (no section headers → MissingSectionHeaderError)
   - All above with CHUTES_ALLOW_MISSING=true (no raise, returns defaults)
   - Valid [auth] without [api] → falls back to default URL
   - Valid full config ([auth] + [api])
   - [api] section present but base_url key missing → falls back
   - CHUTES_API_URL env var override

All 12 tests pass.
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