Skip to content

fix(cli): report invalid JSON config files as a usage error - #635

Merged
cloudsmith-iduffy merged 1 commit into
masterfrom
fix/handle-invalid-json-config-files
Nov 21, 2026
Merged

cloudsmith-iduffy merged 1 commit into
masterfrom
fix/handle-invalid-json-config-files

Conversation

@cloudsmith-iduffy

Copy link
Copy Markdown
Collaborator

Description

Fixes CLOUDSMITH-CLI-2V: cloudsmith repos create crashed with an unhandled JSONDecodeError when the config file had a trailing comma.

The same bare json.load(config_file) was used in 10 commands:

  • repos create / update
  • upstream <format> create / update
  • policy deny|license|vulnerability create / update

A valid JSON document that isn't an object (e.g. [...]) also crashed later on .get.

This PR adds utils.load_json_config_file(file, param_hint), which follows the existing read_privileges_file pattern and raises click.BadParameter for invalid JSON, non-UTF-8 input, or a non-object document. All 10 call sites now use it.

Before:

JSONDecodeError: Illegal trailing comma before end of object: line 4 column 36 (char 97)

After (exit code 2, no API call, nothing sent to Sentry):

Error: Invalid value for 'REPO_CONFIG_FILE': Invalid JSON: Illegal trailing comma before end of object: line 4 column 36 (char 97)

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Refactoring
  • Other (please describe)

Additional Notes

  • New cli/tests/commands/test_config_file_json.py covers 10 commands × 4 bad inputs (trailing comma, empty, non-UTF-8, non-object), 40 cases. All 40 fail on master and pass on this branch.
  • Pre-commit hooks pass on the changed files.
  • Changelog entry added under Unreleased / Fixed.

Generated by Claude Code

@cloudsmith-iduffy
cloudsmith-iduffy requested a review from a team as a code owner November 21, 2026 17:30
Copilot AI balanced review requested due to automatic review settings November 21, 2026 17:30

@claude claude 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.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the changes that turn invalid JSON config files into clear CLI usage errors.

No open issues 🎉

What changed in this PR

This PR replaces unhandled JSON config-file errors with clear CLI usage errors across repository, upstream, and policy commands.

Changes:

  • Adds a shared loader that rejects malformed JSON, decoding errors, and non-object documents with click.BadParameter.
  • Updates all 10 affected command handlers to use the loader.
  • Adds 40 regression cases checking usage errors and preventing API calls, plus a changelog entry.
File Description
cloudsmith_cli/​cli/​utils.py Adds shared JSON config validation.
cloudsmith_cli/​cli/​tests/​commands/​test_config_file_json.py Tests four invalid inputs across 10 commands.
cloudsmith_cli/​cli/​commands/​upstream.py Validates configs for upstream creation and updates.
cloudsmith_cli/​cli/​commands/​repos.py Validates configs for repository creation and updates.
cloudsmith_cli/​cli/​commands/​policy/​vulnerability.py Uses shared validation for vulnerability policies.
cloudsmith_cli/​cli/​commands/​policy/​license.py Uses shared validation for license policies.
cloudsmith_cli/​cli/​commands/​policy/​deny.py Uses shared validation for deny policies.
CHANGELOG.md Documents the improved config-file error handling.

Review effort: Balanced

`repos`, `upstream` and `policy` create/update loaded their config file
with a bare `json.load`, so a malformed file (e.g. a trailing comma)
crashed with an unhandled `JSONDecodeError`, and a non-object document
crashed on `.get`. Add `utils.load_json_config_file`, which raises
`click.BadParameter` for both cases, and use it in all 10 call sites.

Sentry: CLOUDSMITH-CLI-2V

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MwJPoDcCsm7Cp96AeCALZ3
@cloudsmith-iduffy
cloudsmith-iduffy force-pushed the fix/handle-invalid-json-config-files branch from 2ed7206 to 9603649 Compare November 21, 2026 23:51
@cloudsmith-iduffy
cloudsmith-iduffy enabled auto-merge (squash) November 21, 2026 23:51
@cloudsmith-iduffy
cloudsmith-iduffy merged commit 31bbadd into master Nov 21, 2026
19 checks passed
@cloudsmith-iduffy
cloudsmith-iduffy deleted the fix/handle-invalid-json-config-files branch November 21, 2026 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants