Skip to content

fix(utils): narrow bare except Exception in merge_json_files - #4189

Merged
mnriem merged 1 commit into
github:mainfrom
Noor-ul-ain001:fix/merge-json-files-narrow-except
Aug 19, 2026
Merged

fix(utils): narrow bare except Exception in merge_json_files#4189
mnriem merged 1 commit into
github:mainfrom
Noor-ul-ain001:fix/merge-json-files-narrow-except

Conversation

@Noor-ul-ain001

Copy link
Copy Markdown
Contributor

Summary

merge_json_files's read of the existing JSON file wraps json5.load in a bare except Exception as e: that returns None (meaning "preserve existing settings, skip merge") on any failure — including a real programming bug like a TypeError/AttributeError, which is silently treated identically to an expected I/O or parse error and, without verbose, surfaces nothing at all.

The only expected outcomes for this block are:

  • OSError — the file is inaccessible (already special-cased for FileNotFoundError just above)
  • ValueError — malformed JSON5 (json5's decode error is a ValueError subclass)

Anything else should propagate instead of being swallowed as if it were a parse failure.

This is the same bug, same fix shape, as its caller handle_vscode_settings, whose own bare except Exception was narrowed to (OSError, ValueError, KeyError) in commit 16f4577 (#3844) with the identical rationale — "let programming errors like TypeError or AttributeError propagate while still handling expected I/O and parse errors gracefully." That PR's own regression test monkeypatched merge_json_files itself to prove the caller's narrowing works, but the callee it monkeypatched around still had the original bare-except bug.

Test plan

  • Added test_merge_json_files_propagates_programming_errors to tests/test_merge.py, monkeypatching json5.load to raise TypeError and asserting it propagates out of merge_json_files instead of returning None
  • Verified the new test fails without the fix (DID NOT RAISE <class 'TypeError'>) and passes with it
  • pytest tests/test_merge.py — 13 passed
  • ruff check on both changed files — clean

🤖 Generated with Claude Code

merge_json_files's read of the existing JSON file caught bare
`Exception` around `json5.load`, so a real bug there (e.g. a
`TypeError`/`AttributeError`) was silently treated the same as a normal
parse failure -- `None` returned, existing settings preserved untouched,
nothing surfaced unless `verbose`. Only `OSError` (inaccessible file) and
`ValueError` (malformed JSON5 -- json5's decode error is a `ValueError`
subclass) are expected outcomes here; anything else should propagate.

Same bug, same fix shape, as the caller `handle_vscode_settings`, whose
own bare `except Exception` was just narrowed to `(OSError, ValueError,
KeyError)` in commit 16f4577 (PR github#3844) with the same rationale
("let programming errors like TypeError or AttributeError propagate").
That PR's own regression test monkeypatched `merge_json_files` to prove
the caller's narrowing works; this fixes and tests the callee itself,
which still had the original bare-except bug.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

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.

Pull request overview

Narrows JSON merge error handling so unexpected programming errors propagate.

Changes:

  • Catches only expected I/O and parse errors.
  • Adds a regression test for TypeError propagation.
Show a summary per file
File Description
src/specify_cli/_utils.py Narrows the exception handler.
tests/test_merge.py Tests unexpected error propagation.

Review details

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

@mnriem
mnriem merged commit 92e8ab5 into github:main Aug 19, 2026
14 checks passed
@mnriem

mnriem commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

Thank you!

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.

3 participants