Skip to content

Raise ConfigurationError when config.json is malformed - #143

Merged
rquidute merged 1 commit into
project-chip:v2.17-cli-developfrom
rquidute:fix/1140-malformed-config-error
Nov 13, 2026
Merged

rquidute merged 1 commit into
project-chip:v2.17-cli-developfrom
rquidute:fix/1140-malformed-config-error

Conversation

@rquidute

Copy link
Copy Markdown
Contributor

Summary

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.

Closes #1140

Testing

  • pytest tests/test_config.py: 39 passed

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
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

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: Passed 5

Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: raising ConfigurationError for malformed config.json files.
Description check Passed 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 Passed 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 Passed 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 Passed 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.

Share

@mergify

mergify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

This pull request is merged.

@rquidute rquidute self-assigned this Nov 11, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Nitpick comments: 1

tests/test_config.py: 1

244-245: Trivial · Maintainability & Code Quality · Quick win

Test the import-time CLI error path.

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.

Comment thread th_cli/config.py
except Exception as e:
CLIError(f"Could not load config from {config_path}: {e}")
continue
raise ConfigurationError(f"Could not load config from {config_path}: {e}") from e

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Security & Privacy · Quick win

Supported by static analysis

Script executed:

sed -n 1,170p th_cli/config.py
git diff adb4fedd5e86fa4a7b81d4e80ea1ff6331942b69 e213096057383a3fb7852cb318075579bfc697e5 -- th_cli/config.py
rg -n -i 'password|secret|wifi' th_cli/config.py

Repository: project-chip/certification-tool-cli

Length of output: 6729


Script executed:

git diff adb4fedd5e86fa4a7b81d4e80ea1ff6331942b69 e213096057383a3fb7852cb318075579bfc697e5 -- th_cli/exceptions.py pyproject.toml
cat -n th_cli/config.py | sed -n '60,155p'
cat -n th_cli/exceptions.py
rg -n -i -e 'config\.json' -e 'wifi.*password' -e 'password.*wifi' -e 'pydantic' -- th_cli tests README.md pyproject.toml config.json.example || test "$?" -eq 1
rg --files -g '*config*.json*' -g '*lock*' -g 'pyproject.toml' -g 'AGENTS.md' -g 'CLAUDE.md'

Repository: project-chip/certification-tool-cli

Length of output: 13739


Web query:

Pydantic v2 model_validate_json invalid JSON validation error input_value truncation hide_input_in_errors

Result:

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.util
s = 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

Repository: project-chip/certification-tool-cli

Length of output: 11444


Web query:

Pydantic 2 model_validate_json invalid json validation error input_value json_invalid example string

Result:

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.

@greens

greens commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

I think you should ignore the AI nitpick.

@rquidute
rquidute merged commit bfba92a into project-chip:v2.17-cli-develop Nov 13, 2026
6 checks passed
@rquidute
rquidute deleted the fix/1140-malformed-config-error branch November 13, 2026 13:10
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.

2 participants