You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Raise ConfigurationError when config.json is malformed - #143
load_config() built a CLIError for a config.json that failed to parse or validate, but never raised it. The CLI silently fell through to the defaults (hostname: localhost), so a malformed config sent commands to the wrong backend with no indication of the cause.
Raise ConfigurationError("Could not load config from <path>: <error>") instead of discarding the error. A config.json that exists but is broken no longer falls through to later search paths, the example file or localhost. A missing config file still falls back as before.
config = load_config() runs at import time, before click is running, so the error is caught there, shown the way click would show it, and the CLI exits with code 1 instead of a raw traceback.
Replace test_skips_malformed_config_json (which enforced the silent skip) with test_raises_on_malformed_config_json, asserting the error is raised and names the bad file even when a valid config exists in a later search path.
Behavior change: the CLI now exits at startup (including --help) when a config.json is malformed.
load_config() built a CLIError for a config.json that failed to parse
or validate but never raised it, so the CLI silently fell through to
the defaults (hostname: localhost). Raise ConfigurationError naming the
offending file instead, and report it cleanly when it happens at import
time. A missing config file still falls back to the example/defaults.
Closes #1140
When an existing config.json cannot be loaded, load_config() now raises ConfigurationError with the file path and original cause instead of continuing to search other locations. Module initialization catches the error, displays it, and exits with its exit_code. The updated test checks that a malformed file in the first search directory raises the error even when a later directory contains a valid config.
Suggested reviewers:antonio-amjr
Severity of issue fixed: Medium
Merge Risk: Low · up to e2130
A malformed config.json now stops the CLI with a clear error, as intended. The error text may echo parts of the file contents, which could include a password, to the terminal or captured logs. Sanitizing the message before merge is a small, low-risk fix.
Pre-merge checks: 5
Check name
Status
Explanation
Title check
The title clearly and concisely describes the main change: raising ConfigurationError for malformed config.json files.
Description check
The description explains the configuration error handling change, startup behavior, test updates, and linked issue. It is directly related to the changeset.
Linked Issues check
Issue #1140 requires malformed existing config.json files to be surfaced instead of skipped. load_config() now raises ConfigurationError with the file path and parse or validation error. The updat…
Out of Scope Changes check
The changes stay within issue #1140. The exception type change, import-time Click-style reporting, and startup exit directly support malformed configuration handling. The test change verifies the requ…
Docstring Coverage
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Linked Issues check
Explanation
Issue #1140 requires malformed existing config.json files to be surfaced instead of skipped. load_config() now raises ConfigurationError with the file path and parse or validation error. The updated test verifies that the malformed first candidate raises even when a valid later candidate exists. Import-time handling calls show() and exits with the error code, which supports the reported CLI behavior.
Full details: Out of Scope Changes check
Explanation
The changes stay within issue #1140. The exception type change, import-time Click-style reporting, and startup exit directly support malformed configuration handling. The test change verifies the required behavior. No unrelated production or test changes appear in the reviewed diff.
Full details: Docstring Coverage
Explanation
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 2 files.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
This test calls load_config() after import, so it does not exercise the new startup handler. Add a subprocess test with a malformed config that checks stderr, the absence of a traceback, and exit code 1, including for --help.
Based on learnings: assert the exact Click exit code rather than only a nonzero result.
Source: Learnings
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Organization UI
Review profile: CHILL
Plan: Advanced
Run ID: 3872f687-8646-46d1-b5a8-63bdb29fac81
Commits
Reviewing files that changed from the base of the PR and between adb4fed and e213096.
Files selected for processing: 2
tests/test_config.py
th_cli/config.py
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.
Sources consulted:
- Environment variable to show full input value in errors — GitHub issue 12251 in pydantic/pydantic (link omitted to avoid creating a cross-reference)
- Pydantic V2 truncates validation error messages, how to print the full data? · pydantic/pydantic · Discussion #7733 · GitHub — GitHub discussion 7733 in pydantic/pydantic (link omitted to avoid creating a cross-reference)
- Error Handling — https://pydantic.dev/docs/validation/latest/errors/errors
- Model config — https://pydantic.dev/docs/validation/2.2/usage/model_config/
- Configuration — https://pydantic.dev/docs/validation/latest/api/pydantic/config/
Script executed:
cat -n README.md | sed -n '22,45p;95,175p'
cat -n th_cli/default_config.json | sed -n '1,50p'
cat -n tests/test_config.py | sed -n '190,251p'
cat -n pyproject.toml | sed -n '24,45p'
rg -n -F -e 'load_json_config(' -e 'default_config.json' -e '--config' -- th_cli/commands th_cli/test_run th_cli/utils.py
python3 - <<'PY'import importlib.utils = importlib.util.find_spec("pydantic")if s is None: print("pydantic unavailable")else: import pydantic print("pydantic version:", pydantic.__version__) if hasattr(pydantic.BaseModel, "model_validate_json"): class Probe(pydantic.BaseModel): hostname: str = "localhost" try: Probe.model_validate_json('{"password":"secret",') except pydantic.ValidationError as e: print(str(e))PY
Sources consulted:
- Incorrect "raises" documentation for `BaseModel.model_validate_json` — GitHub issue 9944 in pydantic/pydantic (link omitted to avoid creating a cross-reference)
- Models — https://pydantic.dev/docs/validation/2.12/concepts/models/
- Pydantic model_validate_json · Python for AI tutorial — https://www.krishnaik.in/ai-tutorials/python-for-ai/model-validate-json
- Parsing JSON | VizLearn — https://vizlearn.in/pydantic/parsing_json.html
- Handle BOM during JSON validation — GitHub issue 10233 in pydantic/pydantic (link omitted to avoid creating a cross-reference)
Do not include configuration values in the error message.
If a malformed project file is named config.json in the current directory, Pydantic can include part of its contents, including a Wi-Fi password, in the error. The CLI prints that error to stderr, which may be captured in logs.
Report the path and sanitized error details without input values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
load_config()built aCLIErrorfor aconfig.jsonthat failed to parse or validate, but never raised it. The CLI silently fell through to the defaults (hostname: localhost), so a malformed config sent commands to the wrong backend with no indication of the cause.ConfigurationError("Could not load config from <path>: <error>")instead of discarding the error. Aconfig.jsonthat exists but is broken no longer falls through to later search paths, the example file orlocalhost. A missing config file still falls back as before.config = load_config()runs at import time, before click is running, so the error is caught there, shown the way click would show it, and the CLI exits with code 1 instead of a raw traceback.test_skips_malformed_config_json(which enforced the silent skip) withtest_raises_on_malformed_config_json, asserting the error is raised and names the bad file even when a valid config exists in a later search path.Behavior change: the CLI now exits at startup (including
--help) when aconfig.jsonis malformed.Closes #1140
Testing
pytest tests/test_config.py: 39 passed