Skip to content

Commit

Permalink
Trap all problems with reading repo index file
Browse files Browse the repository at this point in the history
If it's got malformed contents we still want to be able
to read a butler without using the alias.
  • Loading branch information
timj committed Jun 7, 2023
1 parent 79aaa20 commit 5f1a668
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion python/lsst/daf/butler/_butlerConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(
# might not refer explicitly to a directory and we have
# check below to guess that.
other = str(ButlerRepoIndex.get_repo_uri(other, True))
except FileNotFoundError:
except Exception:
pass

Check warning on line 93 in python/lsst/daf/butler/_butlerConfig.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/_butlerConfig.py#L92-L93

Added lines #L92 - L93 were not covered by tests
if other != original_other:
resolved_alias = True
Expand Down
10 changes: 7 additions & 3 deletions python/lsst/daf/butler/_butlerRepoIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def _read_repository_index(cls, index_uri: ResourcePathExpression) -> Config:
except FileNotFoundError as e:
# More explicit error message.
raise FileNotFoundError(f"Butler repository index file not found at {uri}.") from e
except Exception as e:
raise RuntimeError(
f"Butler repository index file at {uri} could not be read: {type(e).__qualname__} {e}"
) from e
cls._cache[uri] = repo_index

return repo_index
Expand Down Expand Up @@ -136,7 +140,7 @@ def get_known_repos(cls) -> Set[str]:
"""
try:
repo_index = cls._read_repository_index_from_environment()
except (FileNotFoundError, KeyError):
except Exception:
return set()
return set(repo_index)

Expand All @@ -153,7 +157,7 @@ def get_failure_reason(cls) -> str:
"""
try:
cls._read_repository_index_from_environment()
except (FileNotFoundError, KeyError) as e:
except Exception as e:
return str(e)
return ""

Expand Down Expand Up @@ -189,7 +193,7 @@ def get_repo_uri(cls, label: str, return_label: bool = False) -> ResourcePath:
"""
try:
repo_index = cls._read_repository_index_from_environment()
except KeyError:
except Exception:
if return_label:
return ResourcePath(label, forceAbsolute=False)
raise
Expand Down
7 changes: 7 additions & 0 deletions tests/test_butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,13 @@ def testConstructor(self) -> None:
with unittest.mock.patch.dict(os.environ, {"DAF_BUTLER_REPOSITORY_INDEX": str(temp_file)}):
with self.assertRaisesRegex(FileNotFoundError, "(no known aliases)"):
Butler("label")
with ResourcePath.temporary_uri(suffix=suffix) as temp_file:
# Now with bad contents.
with open(temp_file.ospath, "w") as fh:
print("'", file=fh)
with unittest.mock.patch.dict(os.environ, {"DAF_BUTLER_REPOSITORY_INDEX": str(temp_file)}):
with self.assertRaisesRegex(FileNotFoundError, "(no known aliases:.*could not be read)"):
Butler("label")
with unittest.mock.patch.dict(os.environ, {"DAF_BUTLER_REPOSITORY_INDEX": "file://not_found/x.yaml"}):
with self.assertRaises(FileNotFoundError):
Butler.get_repo_uri("label")
Expand Down

0 comments on commit 5f1a668

Please sign in to comment.