Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 6e86c93

Browse files
weitaitingJon Wayne Parrott
authored andcommitted
Add error message to handle case of empty string or missing file for GOOGLE_APPLICATION_CREDENTIALS (#188)
Fixes #161
1 parent 6a3f0ec commit 6e86c93

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

google/auth/_default.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ def _load_credentials_from_file(filename):
5858
5959
Raises:
6060
google.auth.exceptions.DefaultCredentialsError: if the file is in the
61-
wrong format.
61+
wrong format or is missing.
6262
"""
63+
if not os.path.exists(filename):
64+
raise exceptions.DefaultCredentialsError(
65+
'File {} was not found.'.format(filename))
66+
6367
with io.open(filename, 'r') as file_obj:
6468
try:
6569
info = json.load(file_obj)

tests/test__default.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@
4343
mock.sentinel.credentials, mock.sentinel.project_id), autospec=True)
4444

4545

46+
def test__load_credentials_from_missing_file():
47+
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
48+
_default._load_credentials_from_file('')
49+
50+
assert excinfo.match(r'not found')
51+
52+
4653
def test__load_credentials_from_file_invalid_json(tmpdir):
4754
jsonfile = tmpdir.join('invalid.json')
4855
jsonfile.write('{')

0 commit comments

Comments
 (0)