Skip to content

Commit b309c80

Browse files
refactor: narrow overly broad except Exception catches to specific exception types
- base.py:137: Replace 'except Exception' with 'except (OSError, ImportError)' for filesystem initialization - base.py:146: Replace 'except Exception' with 'except (OSError, json.JSONDecodeError)' for config loading - sample_metadata.py:1867: Replace 'except Exception' with 'except (KeyError, NameError, SyntaxError, TypeError, AttributeError)' for pandas eval queries - map_params.py:45: Replace 'except Exception' with 'except (ImportError, ModuleNotFoundError, AttributeError)' for basemap provider initialization These changes prevent silent masking of unexpected bugs by only catching the specific exception types that each code block is designed to handle. Improves debuggability without altering intended error-handling behavior. Fixes: Narrow overly broad exception handlers
1 parent 49fcc68 commit b309c80

3 files changed

Lines changed: 4 additions & 4 deletions

File tree

malariagen_data/anoph/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def __init__(
134134
storage_options = dict()
135135
try:
136136
self._fs, self._base_path = _init_filesystem(self._url, **storage_options)
137-
except Exception as exc: # pragma: no cover
137+
except (OSError, ImportError) as exc: # pragma: no cover
138138
raise IOError(
139139
"An error occurred establishing a connection to the storage system. Please see the nested exception for more details."
140140
) from exc
@@ -143,7 +143,7 @@ def __init__(
143143
try:
144144
with self.open_file(self._config_path) as f:
145145
self._config = json.load(f)
146-
except Exception as exc: # pragma: no cover
146+
except (OSError, json.JSONDecodeError) as exc: # pragma: no cover
147147
if (isinstance(exc, OSError) and "forbidden" in str(exc).lower()) or (
148148
getattr(exc, "status", None) == 403
149149
):

malariagen_data/anoph/map_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def get_basemap_abbrevs() -> dict:
4242
for key, provider_fn in _basemap_abbrev_candidates.items():
4343
try:
4444
_basemap_abbrevs[key] = provider_fn()
45-
except Exception:
45+
except (ImportError, ModuleNotFoundError, AttributeError):
4646
warnings.warn(
4747
f"Basemap provider {key!r} is not available and will be skipped.",
4848
stacklevel=2,

malariagen_data/anoph/sample_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1864,7 +1864,7 @@ def _locate_cohorts(*, cohorts, data, min_cohort_size):
18641864
for coh, query in cohorts.items():
18651865
try:
18661866
loc_coh = data.eval(query).values
1867-
except Exception as e:
1867+
except (KeyError, NameError, SyntaxError, TypeError, AttributeError) as e:
18681868
raise ValueError(
18691869
f"Invalid query for cohort {coh!r}: {query!r}. Error: {e}"
18701870
) from e

0 commit comments

Comments
 (0)