Skip to content

Commit 16f4577

Browse files
fix: narrow bare except Exception in VS Code settings merge (#3844)
* fix: narrow bare except Exception in VS Code settings merge Replace overly broad except Exception with (OSError, ValueError, KeyError) to let programming errors like TypeError or AttributeError propagate while still handling expected I/O and parse errors gracefully. * test: verify programming errors propagate through handle_vscode_settings The narrow exception change from 'except Exception' to 'except (OSError, ValueError, KeyError)' was not covered by a regression test. Add a test that monkeypatches merge_json_files to raise TypeError and verifies it propagates rather than being swallowed.
1 parent 2290229 commit 16f4577

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/specify_cli/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def atomic_write_json(target_file: Path, payload: dict[str, Any]) -> None:
213213
shutil.copy2(sub_item, dest_file)
214214
log("Copied (no existing settings.json):", "blue")
215215

216-
except Exception as e:
216+
except (OSError, ValueError, KeyError) as e:
217217
log(f"Warning: Could not merge settings: {e}", "yellow")
218218
if not dest_file.exists():
219219
shutil.copy2(sub_item, dest_file)

tests/test_merge.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import stat
22

3+
import pytest
4+
35
from specify_cli import merge_json_files
46
from specify_cli import handle_vscode_settings
57

@@ -188,3 +190,25 @@ def test_handle_vscode_settings_preserves_mode_on_atomic_write(tmp_path):
188190

189191
after_mode = stat.S_IMODE(dest_file.stat().st_mode)
190192
assert after_mode == before_mode
193+
194+
195+
def test_handle_vscode_settings_propagates_programming_errors(tmp_path):
196+
"""Unexpected programming errors (TypeError) must propagate, not be silently swallowed."""
197+
vscode_dir = tmp_path / ".vscode"
198+
vscode_dir.mkdir()
199+
dest_file = vscode_dir / "settings.json"
200+
dest_file.write_text('{"a": 1}\n', encoding="utf-8")
201+
template_file = tmp_path / "template_settings.json"
202+
template_file.write_text('{"b": 2}\n', encoding="utf-8")
203+
204+
import specify_cli._utils as utils_mod
205+
original_merge = utils_mod.merge_json_files
206+
utils_mod.merge_json_files = lambda *a, **kw: (_ for _ in ()).throw(TypeError("boom"))
207+
try:
208+
with pytest.raises(TypeError):
209+
handle_vscode_settings(
210+
template_file, dest_file, "settings.json",
211+
verbose=False, tracker=None,
212+
)
213+
finally:
214+
utils_mod.merge_json_files = original_merge

0 commit comments

Comments
 (0)