Skip to content

Commit d77c331

Browse files
authored
Merge pull request #1226 from kunal-10-cloud/narrow-exception-handlers
refactor: narrow overly broad except Exception catches to specific exception types
2 parents 8d607c2 + 36cadec commit d77c331

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
@@ -135,7 +135,7 @@ def __init__(
135135
storage_options = dict()
136136
try:
137137
self._fs, self._base_path = _init_filesystem(self._url, **storage_options)
138-
except Exception as exc: # pragma: no cover
138+
except (OSError, ImportError) as exc: # pragma: no cover
139139
raise IOError(
140140
"An error occurred establishing a connection to the storage system. Please see the nested exception for more details."
141141
) from exc
@@ -144,7 +144,7 @@ def __init__(
144144
try:
145145
with self.open_file(self._config_path) as f:
146146
self._config = json.load(f)
147-
except Exception as exc: # pragma: no cover
147+
except (OSError, json.JSONDecodeError) as exc: # pragma: no cover
148148
if (isinstance(exc, OSError) and "forbidden" in str(exc).lower()) or (
149149
getattr(exc, "status", None) == 403
150150
):

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, 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)