2222import logging
2323import os
2424
25+ import six
26+
2527from google .auth import environment_vars
2628from google .auth import exceptions
2729import google .auth .transport ._http_client
@@ -67,9 +69,11 @@ def _load_credentials_from_file(filename):
6769 with io .open (filename , 'r' ) as file_obj :
6870 try :
6971 info = json .load (file_obj )
70- except ValueError as exc :
71- raise exceptions .DefaultCredentialsError (
72- 'File {} is not a valid json file.' .format (filename ), exc )
72+ except ValueError as caught_exc :
73+ new_exc = exceptions .DefaultCredentialsError (
74+ 'File {} is not a valid json file.' .format (filename ),
75+ caught_exc )
76+ six .raise_from (new_exc , caught_exc )
7377
7478 # The type key should indicate that the file is either a service account
7579 # credentials file or an authorized user credentials file.
@@ -80,10 +84,11 @@ def _load_credentials_from_file(filename):
8084
8185 try :
8286 credentials = _cloud_sdk .load_authorized_user_credentials (info )
83- except ValueError as exc :
84- raise exceptions .DefaultCredentialsError (
85- 'Failed to load authorized user credentials from {}' .format (
86- filename ), exc )
87+ except ValueError as caught_exc :
88+ msg = 'Failed to load authorized user credentials from {}' .format (
89+ filename )
90+ new_exc = exceptions .DefaultCredentialsError (msg , caught_exc )
91+ six .raise_from (new_exc , caught_exc )
8792 # Authorized user credentials do not contain the project ID.
8893 return credentials , None
8994
@@ -93,10 +98,11 @@ def _load_credentials_from_file(filename):
9398 try :
9499 credentials = (
95100 service_account .Credentials .from_service_account_info (info ))
96- except ValueError as exc :
97- raise exceptions .DefaultCredentialsError (
98- 'Failed to load service account credentials from {}' .format (
99- filename ), exc )
101+ except ValueError as caught_exc :
102+ msg = 'Failed to load service account credentials from {}' .format (
103+ filename )
104+ new_exc = exceptions .DefaultCredentialsError (msg , caught_exc )
105+ six .raise_from (new_exc , caught_exc )
100106 return credentials , info .get ('project_id' )
101107
102108 else :
0 commit comments