Reputation: 93
Most guides & posts talk about the reverse where you decide which exceptions to retry on ahead of time. Perhaps I'm thinking about it the wrong way, but for example, I'm trying to get google service account credentials
def get_google_credentials(self, google_creds_dict):
SCOPES = [
'https://www.googleapis.com/auth/drive.readonly',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/spreadsheets',
]
credentials = service_account.Credentials.from_service_account_info(google_creds_dict, scopes=SCOPES)
return credentials
Since I know it returns a ValueError
, I think I'd only want to retry on other exceptions
Upvotes: 0
Views: 531
Reputation: 782105
Use an except <type>:
block for the specific exceptions you want to pass through, and then a general except:
for all the ones that should be retried. Then put this in a while
loop to get it to keep trying.
import time
def get_google_credentials(self, google_creds_dict):
SCOPES = [
'https://www.googleapis.com/auth/drive.readonly',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/spreadsheets',
]
while True:
try:
return service_account.Credentials.from_service_account_info(google_creds_dict, scopes=SCOPES)
except ValueError as e:
# pass ValueError through
raise e from None
except:
# retry on any other error
time.sleep(1)
Upvotes: 1