10000 Adding back "wrapped" exceptions in Refresh/Transport errors. · sktt/google-auth-library-python@0a93e87 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a93e87

Browse files
committed
Adding back "wrapped" exceptions in Refresh/Transport errors.
1 parent ae5d3a4 commit 0a93e87

File tree

7 files changed

+17
-12
lines changed

7 files changed

+17
-12
lines changed

google/auth/_default.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def _load_credentials_from_file(filename):
7171
info = json.load(file_obj)
7272
except ValueError as caught_exc:
7373
new_exc = exceptions.DefaultCredentialsError(
74-
'File {} is not a valid json file.'.format(filename))
74+
'File {} is not a valid json file.'.format(filename),
75+
caught_exc)
7576
six.raise_from(new_exc, caught_exc)
7677

7778
# The type key should indicate that the file is either a service account
@@ -84,9 +85,9 @@ def _load_credentials_from_file(filename):
8485
try:
8586
credentials = _cloud_sdk.load_authorized_user_credentials(info)
8687
except ValueError as caught_exc:
87-
new_exc = exceptions.DefaultCredentialsError(
88-
'Failed to load authorized user credentials from {}'.format(
89-
filename))
88+
msg = 'Failed to load authorized user credentials from {}'.format(
89+
filename)
90+
new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
9091
six.raise_from(new_exc, caught_exc)
9192
# Authorized user credentials do not contain the project ID.
9293
return credentials, None
@@ -98,9 +99,9 @@ def _load_credentials_from_file(filename):
9899
credentials = (
99100
service_account.Credentials.from_service_account_info(info))
100101
except ValueError as caught_exc:
101-
new_exc = exceptions.DefaultCredentialsError(
102-
'Failed to load service account credentials from {}'.format(
103-
filename))
102+
msg = 'Failed to load service account credentials from {}'.format(
103+
filename)
104+
new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
104105
six.raise_from(new_exc, caught_exc)
105106
return credentials, info.get('project_id')
106107

google/auth/compute_engine/credentials.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def refresh(self, request):
9292
request,
9393
service_account=self._service_account_email)
9494
except exceptions.TransportError as caught_exc:
95-
new_exc = exceptions.RefreshError()
95+
new_exc = exceptions.RefreshError(caught_exc)
9696
six.raise_from(new_exc, caught_exc)
9797

9898
@property

google/auth/transport/_http_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def __call__(self, url, method='GET', body=None, headers=None,
106106
return Response(response)
107107

108108
except (http_client.HTTPException, socket.error) as caught_exc:
109-
new_exc = exceptions.TransportError()
109+
new_exc = exceptions.TransportError(caught_exc)
110110
six.raise_from(new_exc, caught_exc)
111111

112112
finally:

google/auth/transport/requests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def __call__(self, url, method='GET', body=None, headers=None,
118118
**kwargs)
119119
return _Response(response)
120120
except requests.exceptions.RequestException as caught_exc:
121-
new_exc = exceptions.TransportError()
121+
new_exc = exceptions.TransportError(caught_exc)
122122
six.raise_from(new_exc, caught_exc)
123123

124124

google/auth/transport/urllib3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def __call__(self, url, method='GET', body=None, headers=None,
133133
method, url, body=body, headers=headers, **kwargs)
134134
return _Response(response)
135135
except urllib3.exceptions.HTTPError as caught_exc:
136-
new_exc = exceptions.TransportError()
136+
new_exc = exceptions.TransportError(caught_exc)
137137
six.raise_from(new_exc, caught_exc)
138138

139139

tests/compute_engine/test_credentials.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ def test_refresh_success(self, get, utcnow):
7575
def test_refresh_error(self, get):
7676
get.side_effect = exceptions.TransportError('http error')
7777

78-
with pytest.raises(exceptions.RefreshError):
78+
with pytest.raises(exceptions.RefreshError) as excinfo:
7979
self.credentials.refresh(None)
8080

81+
assert excinfo.match(r'http error')
82+
8183
@mock.patch('google.auth.compute_engine._metadata.get', autospec=True)
8284
def test_before_request_refreshes(self, get):
8385
get.side_effect = [{

tests/test__default.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def test__load_credentials_from_file_authorized_user_bad_format(tmpdir):
8585
_default._load_credentials_from_file(str(filename))
8686

8787
assert excinfo.match(r'Failed to load authorized user')
88+
assert excinfo.match(r'missing fields')
8889

8990

9091
def test__load_credentials_from_file_service_account():
@@ -102,6 +103,7 @@ def test__load_credentials_from_file_service_account_bad_format(tmpdir):
102103
_default._load_credentials_from_file(str(filename))
103104

104105
assert excinfo.match(r'Failed to load service account')
106+
assert excinfo.match(r'missing fields')
105107

106108

107109
@mock.patch.dict(os.environ, {}, clear=True)

0 commit comments

Comments
 (0)
0