Skip to content

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

Open
Noor-ul-ain001 wants to merge 1 commit into
github:mainfrom
Noor-ul-ain001:fix/merge-json-files-narrow-except
Open

fix(utils): narrow bare except Exception in merge_json_files#4189
Noor-ul-ain001 wants to merge 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>
@Noor-ul-ain001
Noor-ul-ain001 requested a review from mnriem as a code owner August 18, 2026 16:39
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