Added error handling, send email to user if adding fails
This commit is contained in:
+13
-3
@@ -125,7 +125,7 @@ def admin_send_email_signupees(list, subject, content):
|
||||
send_email(to.email, subject, markdown.markdown(content), True)
|
||||
|
||||
|
||||
def add_to_mailinglist(email):
|
||||
def add_to_mailinglist(email: str):
|
||||
try:
|
||||
# get data
|
||||
SCOPES = ["https://www.googleapis.com/auth/admin.directory.group"]
|
||||
@@ -140,6 +140,16 @@ def add_to_mailinglist(email):
|
||||
service = build("admin", "directory_v1", credentials=credentials)
|
||||
service.members().insert(groupKey=GROUP_KEY, body={"email": email}).execute()
|
||||
except HttpError as err:
|
||||
logging.exception("Something fukd: {}".format(err.error_details))
|
||||
return err.error_details.message
|
||||
# Already in list, do nothing
|
||||
if err.status_code == 409:
|
||||
pass
|
||||
# Something went wrong, send notification to maintainer.
|
||||
else:
|
||||
logging.exception("Failed adding user to list")
|
||||
|
||||
to = "ilari.ojakorpi@sahkoinsinoorikilta.fi"
|
||||
subject = "Web error: Failed adding to google groups"
|
||||
body = "Error code: {}\nError details: {}\nEmail that was not added: {}\n\nAdd user manually to jäsenet groups.".format(err.status_code, err.error_details, email)
|
||||
|
||||
send_email(to, subject, body)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user