Skip to content

Commit

Permalink
added better error checking to exit gracefully if a gh token is inval…
Browse files Browse the repository at this point in the history
…id, has insufficient scope, or other errors. This is after spending way too much time debugging only to realize that I used the wrong token with an insufficient scope. (#89)

Signed-off-by: Dawn M. Foster <[email protected]>
  • Loading branch information
geekygirldawn authored May 19, 2023
1 parent 1dbea95 commit a8037a3
Showing 1 changed file with 24 additions and 0 deletions.
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

0 comments on commit a8037a3

Please sign in to comment.