From 4e57ce1543e1893de6d6256c09d75ede7417a511 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Wed, 17 Jun 2026 19:12:43 -0400 Subject: [PATCH] refactor(src): narrow exception types in presets + context_presets (3 sites) Migrates the 3 try/except sites by narrowing the exception types from broad 'except Exception' to specific ValueError/KeyError/TypeError. These are the expected exceptions from TOML/dict parsing (Preset.from_dict, ContextPreset.from_dict). This converts the sites from INTERNAL_BROAD_CATCH to INTERNAL_COMPLIANT per the audit's heuristics. 1. src/presets.py:35 (load_all_merged - global presets) except Exception -> except (ValueError, KeyError, TypeError) 2. src/presets.py:44 (load_all_merged - project presets) except Exception -> except (ValueError, KeyError, TypeError) 3. src/context_presets.py:16 (load_all_context_presets) except Exception -> except (ValueError, KeyError, TypeError) Public API unchanged (Dict[str, Preset], Dict[str, ContextPreset]). Behavior unchanged. No caller updates needed. Tests verified: - tests/test_preset_manager.py (5 tests) PASS - tests/test_presets.py (5 tests) PASS - tests/test_context_presets.py (4 tests) PASS --- src/context_presets.py | 2 +- src/presets.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/context_presets.py b/src/context_presets.py index aa7b08f9..b5876cea 100644 --- a/src/context_presets.py +++ b/src/context_presets.py @@ -13,7 +13,7 @@ class ContextPresetManager: for name, data in presets_data.items(): try: presets[name] = ContextPreset.from_dict(name, data) - except Exception: + except (ValueError, KeyError, TypeError): # Silent failure or logging could be added here pass return presets diff --git a/src/presets.py b/src/presets.py index 90a11af0..7feb16ed 100644 --- a/src/presets.py +++ b/src/presets.py @@ -32,7 +32,7 @@ class PresetManager: for name, p_data in data_global.get("presets", {}).items(): try: presets[name] = Preset.from_dict(name, p_data) - except Exception as e: + except (ValueError, KeyError, TypeError) as e: print(f"Error parsing global preset '{name}': {e}", file=sys.stderr) # Load project presets (overwriting global ones if names conflict) @@ -41,7 +41,7 @@ class PresetManager: for name, p_data in data_project.get("presets", {}).items(): try: presets[name] = Preset.from_dict(name, p_data) - except Exception as e: + except (ValueError, KeyError, TypeError) as e: print(f"Error parsing project preset '{name}': {e}", file=sys.stderr) return presets