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,10 @@ 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+ six .raise_from (new_exc , caught_exc )
7376
7477 # The type key should indicate that the file is either a service account
7578 # credentials file or an authorized user credentials file.
@@ -80,10 +83,11 @@ def _load_credentials_from_file(filename):
8083
8184 try :
8285 credentials = _cloud_sdk .load_authorized_user_credentials (info )
83- except ValueError as exc :
84- raise exceptions .DefaultCredentialsError (
86+ except ValueError as caught_exc :
87+ new_exc = exceptions .DefaultCredentialsError (
8588 'Failed to load authorized user credentials from {}' .format (
86- filename ), exc )
89+ filename ))
90+ six .raise_from (new_exc , caught_exc )
8791 # Authorized user credentials do not contain the project ID.
8892 return credentials , None
8993
@@ -93,10 +97,11 @@ def _load_credentials_from_file(filename):
9397 try :
9498 credentials = (
9599 service_account .Credentials .from_service_account_info (info ))
96- except ValueError as exc :
97- raise exceptions .DefaultCredentialsError (
100+ except ValueError as caught_exc :
101+ new_exc = exceptions .DefaultCredentialsError (
98102 'Failed to load service account credentials from {}' .format (
99- filename ), exc )
103+ filename ))
104+ six .raise_from (new_exc , caught_exc )
100105 return credentials , info .get ('project_id' )
101106
102107 else :
0 commit comments