Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added better error checking to the emails script #89

Merged
merged 1 commit into from
May 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions scripts/elekto_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def get_email(username, api_token):

import requests
import json
import sys
from dateutil.relativedelta import relativedelta

# Set GitHub GraphQL API variables
Expand All @@ -144,6 +145,29 @@ def get_email(username, api_token):
r = requests.post(url=url, json={'query': query, 'variables': variables}, headers=headers)
json_data = json.loads(r.text)

# Error checking
error = False
if r.ok: # the request succeeds and the GH token is valid
# check for and print errors returned from API before exiting
# insufficient scope for your token would be a common error caught here
try:
print("Error:", json_data['errors'][0]['type'])
print(json_data['errors'][0]['message'])
print( "Exiting ...")
error = True
except:
pass # keep going because there are no errors
else: # request failed - often due to invalid GH token
try: # request fails with a message
print("Error:", json_data['message'])
print( "Exiting ...")
except: # request failed for some reason that doesn't generate a message
print("Unknown Error. Exiting ...")
error = True

if error: # Exit after any request or API error
sys.exit(1)

# Get email address
email = None

Expand Down