Skip to content

Commit 955ab69

Browse files
RajeshGovohimanshupr2627sqrrrl
authored
Added try-except block (googleworkspace#229)
* Added try-except block Co-authored-by: Himanshu <[email protected]> Co-authored-by: Steve Bazyl <[email protected]> Co-authored-by: Steve Bazyl <[email protected]>
1 parent 42e682e commit 955ab69

11 files changed

Lines changed: 108 additions & 78 deletions

File tree

apps_script/quickstart/quickstart.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def main():
6666
with open('token.json', 'w') as token:
6767
token.write(creds.to_json())
6868

69-
service = build('script', 'v1', credentials=creds)
70-
71-
# Call the Apps Script API
7269
try:
70+
service = build('script', 'v1', credentials=creds)
71+
72+
# Call the Apps Script API
7373
# Create a new project
7474
request = {'title': 'My Script'}
7575
response = service.projects().create(body=request).execute()

calendar/quickstart/quickstart.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ def main():
6161
orderBy='startTime').execute()
6262
events = events_result.get('items', [])
6363

64-
# Prints the start and name of the next 10 events
6564
if not events:
6665
print('No upcoming events found.')
66+
return
67+
68+
# Prints the start and name of the next 10 events
6769
for event in events:
6870
start = event['start'].get('dateTime', event['start'].get('date'))
6971
print(start, event['summary'])

classroom/quickstart/quickstart.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,21 @@ def main():
4848
# Save the credentials for the next run
4949
with open('token.json', 'w') as token:
5050
token.write(creds.to_json())
51+
5152
try:
5253
service = build('classroom', 'v1', credentials=creds)
5354

5455
# Call the Classroom API
5556
results = service.courses().list(pageSize=10).execute()
5657
courses = results.get('courses', [])
5758

58-
# Prints the names of the first 10 courses.
5959
if not courses:
6060
print('No courses found.')
61-
else:
62-
print('Courses:')
63-
for course in courses:
64-
print(course['name'])
61+
return
62+
# Prints the names of the first 10 courses.
63+
print('Courses:')
64+
for course in courses:
65+
print(course['name'])
6566

6667
except HttpError as error:
6768
print('An error occurred: %s' % error)

docs/quickstart/quickstart.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from google.oauth2.credentials import Credentials
2222
from google_auth_oauthlib.flow import InstalledAppFlow
2323
from googleapiclient.discovery import build
24+
from googleapiclient.errors import HttpError
2425

2526
# If modifying these scopes, delete the file token.json.
2627
SCOPES = ['https://www.googleapis.com/auth/documents.readonly']
@@ -51,12 +52,15 @@ def main():
5152
with open('token.json', 'w') as token:
5253
token.write(creds.to_json())
5354

54-
service = build('docs', 'v1', credentials=creds)
55+
try:
56+
service = build('docs', 'v1', credentials=creds)
5557

56-
# Retrieve the documents contents from the Docs service.
57-
document = service.documents().get(documentId=DOCUMENT_ID).execute()
58+
# Retrieve the documents contents from the Docs service.
59+
document = service.documents().get(documentId=DOCUMENT_ID).execute()
5860

59-
print('The title of the document is: {}'.format(document.get('title')))
61+
print('The title of the document is: {}'.format(document.get('title')))
62+
except HttpError as err:
63+
print(err)
6064

6165

6266
if __name__ == '__main__':

drive/quickstart/quickstart.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,29 @@ def main():
4242
if creds and creds.expired and creds.refresh_token:
4343
creds.refresh(Request())
4444
else:
45-
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
45+
flow = InstalledAppFlow.from_client_secrets_file(
46+
'credentials.json', SCOPES)
4647
creds = flow.run_local_server(port=0)
4748
# Save the credentials for the next run
4849
with open('token.json', 'w') as token:
4950
token.write(creds.to_json())
5051

51-
service = build('drive', 'v3', credentials=creds)
52-
53-
# Call the Drive v3 API
5452
try:
53+
service = build('drive', 'v3', credentials=creds)
54+
55+
# Call the Drive v3 API
5556
results = service.files().list(
5657
pageSize=10, fields="nextPageToken, files(id, name)").execute()
5758
items = results.get('files', [])
5859

59-
# Prints the names and ids of the first 10 files in drive
6060
if not items:
6161
print('No files found.')
62-
else:
63-
print('Files:')
64-
for item in items:
65-
print(u'{0} ({1})'.format(item['name'], item['id']))
66-
62+
return
63+
print('Files:')
64+
for item in items:
65+
print(u'{0} ({1})'.format(item['name'], item['id']))
6766
except HttpError as error:
68-
# TODO(developer) - Handleerrors from drive API.
67+
# TODO(developer) - Handle errors from drive API.
6968
print(f'An error occurred: {error}')
7069

7170

gmail/quickstart/quickstart.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,28 @@ def main():
4242
if creds and creds.expired and creds.refresh_token:
4343
creds.refresh(Request())
4444
else:
45-
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
45+
flow = InstalledAppFlow.from_client_secrets_file(
46+
'credentials.json', SCOPES)
4647
creds = flow.run_local_server(port=0)
4748
# Save the credentials for the next run
4849
with open('token.json', 'w') as token:
4950
token.write(creds.to_json())
5051

51-
service = build('gmail', 'v1', credentials=creds)
52-
53-
# Call the Gmail API
5452
try:
53+
# Call the Gmail API
54+
service = build('gmail', 'v1', credentials=creds)
5555
results = service.users().labels().list(userId='me').execute()
5656
labels = results.get('labels', [])
5757

5858
if not labels:
5959
print('No labels found.')
60-
else:
61-
print('Labels:')
62-
for label in labels:
63-
print(label['name'])
60+
return
61+
print('Labels:')
62+
for label in labels:
63+
print(label['name'])
6464

6565
except HttpError as error:
66-
# TODO(developer) - Handleerrors from gmail API.
66+
# TODO(developer) - Handle errors from gmail API.
6767
print(f'An error occurred: {error}')
6868

6969

people/quickstart/quickstart.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from google.oauth2.credentials import Credentials
2222
from google_auth_oauthlib.flow import InstalledAppFlow
2323
from googleapiclient.discovery import build
24+
from googleapiclient.errors import HttpError
2425

2526
# If modifying these scopes, delete the file token.json.
2627
SCOPES = ['https://www.googleapis.com/auth/contacts.readonly']
@@ -48,21 +49,24 @@ def main():
4849
with open('token.json', 'w') as token:
4950
token.write(creds.to_json())
5051

51-
service = build('people', 'v1', credentials=creds)
52+
try:
53+
service = build('people', 'v1', credentials=creds)
5254

53-
# Call the People API
54-
print('List 10 connection names')
55-
results = service.people().connections().list(
56-
resourceName='people/me',
57-
pageSize=10,
58-
personFields='names,emailAddresses').execute()
59-
connections = results.get('connections', [])
55+
# Call the People API
56+
print('List 10 connection names')
57+
results = service.people().connections().list(
58+
resourceName='people/me',
59+
pageSize=10,
60+
personFields='names,emailAddresses').execute()
61+
connections = results.get('connections', [])
6062

61-
for person in connections:
62-
names = person.get('names', [])
63-
if names:
64-
name = names[0].get('displayName')
65-
print(name)
63+
for person in connections:
64+
names = person.get('names', [])
65+
if names:
66+
name = names[0].get('displayName')
67+
print(name)
68+
except HttpError as err:
69+
print(err)
6670

6771

6872
if __name__ == '__main__':

sheets/quickstart/quickstart.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from google.oauth2.credentials import Credentials
2222
from google_auth_oauthlib.flow import InstalledAppFlow
2323
from googleapiclient.discovery import build
24+
from googleapiclient.errors import HttpError
2425

2526
# If modifying these scopes, delete the file token.json.
2627
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
@@ -52,21 +53,25 @@ def main():
5253
with open('token.json', 'w') as token:
5354
token.write(creds.to_json())
5455

55-
service = build('sheets', 'v4', credentials=creds)
56+
try:
57+
service = build('sheets', 'v4', credentials=creds)
5658

57-
# Call the Sheets API
58-
sheet = service.spreadsheets()
59-
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
60-
range=SAMPLE_RANGE_NAME).execute()
61-
values = result.get('values', [])
59+
# Call the Sheets API
60+
sheet = service.spreadsheets()
61+
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
62+
range=SAMPLE_RANGE_NAME).execute()
63+
values = result.get('values', [])
64+
65+
if not values:
66+
print('No data found.')
67+
return
6268

63-
if not values:
64-
print('No data found.')
65-
else:
6669
print('Name, Major:')
6770
for row in values:
6871
# Print columns A and E, which correspond to indices 0 and 4.
6972
print('%s, %s' % (row[0], row[4]))
73+
except HttpError as err:
74+
print(err)
7075

7176

7277
if __name__ == '__main__':

slides/quickstart/quickstart.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from google.oauth2.credentials import Credentials
2222
from google_auth_oauthlib.flow import InstalledAppFlow
2323
from googleapiclient.discovery import build
24+
from googleapiclient.errors import HttpError
2425

2526
# If modifying these scopes, delete the file token.json.
2627
SCOPES = ['https://www.googleapis.com/auth/presentations.readonly']
@@ -51,17 +52,20 @@ def main():
5152
with open('token.json', 'w') as token:
5253
token.write(creds.to_json())
5354

54-
service = build('slides', 'v1', credentials=creds)
55+
try:
56+
service = build('slides', 'v1', credentials=creds)
5557

56-
# Call the Slides API
57-
presentation = service.presentations().get(
58-
presentationId=PRESENTATION_ID).execute()
59-
slides = presentation.get('slides')
58+
# Call the Slides API
59+
presentation = service.presentations().get(
60+
presentationId=PRESENTATION_ID).execute()
61+
slides = presentation.get('slides')
6062

61-
print('The presentation contains {} slides:'.format(len(slides)))
62-
for i, slide in enumerate(slides):
63-
print('- Slide #{} contains {} elements.'.format(
64-
i + 1, len(slide.get('pageElements'))))
63+
print('The presentation contains {} slides:'.format(len(slides)))
64+
for i, slide in enumerate(slides):
65+
print('- Slide #{} contains {} elements.'.format(
66+
i + 1, len(slide.get('pageElements'))))
67+
except HttpError as err:
68+
print(err)
6569

6670

6771
if __name__ == '__main__':

tasks/quickstart/quickstart.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from google.oauth2.credentials import Credentials
2222
from google_auth_oauthlib.flow import InstalledAppFlow
2323
from googleapiclient.discovery import build
24+
from googleapiclient.errors import HttpError
2425

2526
# If modifying these scopes, delete the file token.json.
2627
SCOPES = ['https://www.googleapis.com/auth/tasks.readonly']
@@ -48,18 +49,22 @@ def main():
4849
with open('token.json', 'w') as token:
4950
token.write(creds.to_json())
5051

51-
service = build('tasks', 'v1', credentials=creds)
52+
try:
53+
service = build('tasks', 'v1', credentials=creds)
5254

53-
# Call the Tasks API
54-
results = service.tasklists().list(maxResults=10).execute()
55-
items = results.get('items', [])
55+
# Call the Tasks API
56+
results = service.tasklists().list(maxResults=10).execute()
57+
items = results.get('items', [])
58+
59+
if not items:
60+
print('No task lists found.')
61+
return
5662

57-
if not items:
58-
print('No task lists found.')
59-
else:
6063
print('Task lists:')
6164
for item in items:
6265
print(u'{0} ({1})'.format(item['title'], item['id']))
66+
except HttpError as err:
67+
print(err)
6368

6469

6570
if __name__ == '__main__':

0 commit comments

Comments
 (0)