fix(vertex): handle paths open() rejects with a bare ValueError

This commit is contained in:
Kolade Fajimi 2026-09-08 07:24:32 +01:00
parent 596cc4c0c2
commit c5f4f124f5
2 changed files with 10 additions and 0 deletions

View file

@ -74,6 +74,9 @@ def _read_json_file(path: str) -> _VertexCredentialsFile:
return VertexCredentialsFileUnreadable(path, f"{e.strerror or e} ({type(e).__name__})")
except UnicodeDecodeError:
return VertexCredentialsFileNotJson(path, "file is not UTF-8 text")
except ValueError as e:
# open() rejects a few paths before touching the filesystem, e.g. "embedded null byte".
return VertexCredentialsFileUnreadable(path, f"{e} ({type(e).__name__})")
parsed: Final = _parse_json_object(contents)
if isinstance(parsed, _NotAJsonObject):

View file

@ -2304,6 +2304,13 @@ class TestVertexCredentialsSource:
assert project_id == "from-inline"
assert from_sa.call_args.args[0] == {"type": "service_account", "project_id": "from-inline"}
def test_a_path_open_rejects_outright_is_reported_not_raised_raw(self):
"""open() rejects some paths with a bare ValueError before any filesystem call."""
with pytest.raises(ValueError, match="Unable to read the vertex credentials file") as exc_info:
VertexBase().load_auth(credentials="creds" + chr(0) + ".json", project_id="p")
assert "embedded null" in str(exc_info.value)
def test_a_file_whose_name_starts_with_a_brace_is_still_read(self, tmp_path):
"""Shape dispatch must not strand a real file that happens to be named like JSON."""
braced = tmp_path / "{vertex}.json"