-
Notifications
You must be signed in to change notification settings - Fork 25
#3523: Standardize all failed email logger error messages - [RH] #3937
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
base: main
Are you sure you want to change the base?
Changes from all commits
943d196
7365748
ff1ae77
dbc5646
0ee9acb
a73f998
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -940,18 +940,22 @@ def test_send_email_failure(self, mock_logger, mock_get_requestor_email, mock_se | |
permissions.user.email = "[email protected]" | ||
permissions.portfolio.organization_name = "Test Portfolio" | ||
|
||
mock_get_requestor_email.return_value = "[email protected]" | ||
mock_get_requestor_email.return_value = MagicMock(name="mock.email") | ||
|
||
# Call function | ||
result = send_portfolio_member_permission_update_email(requestor, permissions) | ||
|
||
# Assertions | ||
mock_logger.warning.assert_called_once_with( | ||
"Could not send email organization member update notification to %s for portfolio: %s", | ||
permissions.user.email, | ||
permissions.portfolio.organization_name, | ||
exc_info=True, | ||
expected_message = ( | ||
"Failed to send organization member update notification email:\n" | ||
f" Requestor Email: {mock_get_requestor_email.return_value}\n" | ||
f" Subject: portfolio_update_subject.txt\n" | ||
f" To: {permissions.user.email}\n" | ||
f" Portfolio: {permissions.portfolio}\n" | ||
f" Error: Email failed" | ||
) | ||
|
||
mock_logger.error.assert_called_once_with(expected_message, exc_info=True) | ||
self.assertFalse(result) | ||
|
||
@patch("registrar.utility.email_invitations._get_requestor_email", side_effect=Exception("Unexpected error")) | ||
|
@@ -1013,18 +1017,22 @@ def test_send_email_failure(self, mock_logger, mock_get_requestor_email, mock_se | |
permissions.user.email = "[email protected]" | ||
permissions.portfolio.organization_name = "Test Portfolio" | ||
|
||
mock_get_requestor_email.return_value = "[email protected]" | ||
mock_get_requestor_email.return_value = MagicMock(name="mock.email") | ||
|
||
# Call function | ||
result = send_portfolio_member_permission_remove_email(requestor, permissions) | ||
|
||
# Assertions | ||
mock_logger.warning.assert_called_once_with( | ||
"Could not send email organization member removal notification to %s for portfolio: %s", | ||
permissions.user.email, | ||
permissions.portfolio.organization_name, | ||
exc_info=True, | ||
expected_message = ( | ||
"Failed to send portfolio member removal email:\n" | ||
f" Requestor Email: {mock_get_requestor_email.return_value}\n" | ||
f" Subject: portfolio_removal_subject.txt\n" | ||
f" To: {permissions.user.email}\n" | ||
f" Portfolio: {permissions.portfolio}\n" | ||
f" Error: Email failed" | ||
) | ||
|
||
mock_logger.error.assert_called_once_with(expected_message, exc_info=True) | ||
self.assertFalse(result) | ||
|
||
@patch("registrar.utility.email_invitations._get_requestor_email", side_effect=Exception("Unexpected error")) | ||
|
@@ -1092,10 +1100,12 @@ def test_send_email_failure(self, mock_logger, mock_get_requestor_email, mock_se | |
result = send_portfolio_invitation_remove_email(requestor, invitation) | ||
|
||
# Assertions | ||
mock_logger.warning.assert_called_once_with( | ||
"Could not send email organization member removal notification to %s for portfolio: %s", | ||
invitation.email, | ||
invitation.portfolio.organization_name, | ||
mock_logger.error.assert_called_once_with( | ||
"Failed to send portfolio invitation removal email:\n" | ||
f" Subject: portfolio_removal_subject.txt\n" | ||
f" To: {invitation.email}\n" | ||
f" Portfolio: {invitation.portfolio.organization_name}\n" | ||
f" Error: Email failed", | ||
exc_info=True, | ||
) | ||
self.assertFalse(result) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,15 @@ def _send_domain_invitation_email(email, requestor_email, domains, requested_use | |
) | ||
except EmailSendingError as err: | ||
domain_names = ", ".join([domain.name for domain in domains]) | ||
logger.error( | ||
"Failed to send domain invitation email:\n" | ||
f" Requestor Email: {requestor_email}\n" | ||
f" Subject: domain_invitation_subject.txt\n" | ||
f" To: {email}\n" | ||
f" Domains: {domain_names}\n" | ||
f" Error: {err}", | ||
exc_info=True, | ||
) | ||
raise EmailSendingError(f"Could not send email invitation to {email} for domains: {domain_names}") from err | ||
|
||
|
||
|
@@ -173,9 +182,15 @@ def _send_domain_invitation_update_emails_to_domain_managers( | |
"date": date.today(), | ||
}, | ||
) | ||
except EmailSendingError: | ||
logger.warning( | ||
f"Could not send email manager notification to {user.email} for domain: {domain.name}", exc_info=True | ||
except EmailSendingError as err: | ||
logger.error( | ||
"Failed to send domain manager update notification email:\n" | ||
f" Requestor Email: {requestor_email}\n" | ||
f" Subject: domain_manager_notification_subject.txt\n" | ||
f" To: {user.email}\n" | ||
f" Domain: {domain.name}\n" | ||
f" Error: {err}", | ||
exc_info=True, | ||
) | ||
all_emails_sent = False | ||
return all_emails_sent | ||
|
@@ -220,11 +235,15 @@ def send_domain_manager_removal_emails_to_domain_managers( | |
"date": date.today(), | ||
}, | ||
) | ||
except EmailSendingError: | ||
logger.warning( | ||
"Could not send notification email to %s for domain %s", | ||
user.email, | ||
domain.name, | ||
except EmailSendingError as err: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be out of scope but I noticed when we fail to email domain managers that a manager has been added, we add an alert saying so But when we remove a domain manager and fail to email the other domain managers, that update doesn't show Are the alert error messages also in scope of this ticket or is that separate? We can create another ticket if it is out of scope! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's create another ticket as this one is focused just on logs and not the error displays! Another great catch btw!!! 💯 |
||
logger.error( | ||
"Failed to send domain manager deleted notification email:\n" | ||
f" User that did the removing: {removed_by_user}\n" | ||
f" Domain manager removed: {manager_removed_email}\n" | ||
f" Subject: domain_manager_deleted_notification_subject.txt\n" | ||
f" To: {user.email}\n" | ||
f" Domain: {domain.name}\n" | ||
f" Error: {err}", | ||
exc_info=True, | ||
) | ||
all_emails_sent = False | ||
|
@@ -265,6 +284,15 @@ def send_portfolio_invitation_email(email: str, requestor, portfolio, is_admin_i | |
}, | ||
) | ||
except EmailSendingError as err: | ||
logger.error( | ||
"Failed to send portfolio invitation email:\n" | ||
f" Requestor Email: {requestor_email}\n" | ||
f" Subject: portfolio_invitation_subject.txt\n" | ||
f" To: {email}\n" | ||
f" Portfolio: {portfolio}\n" | ||
f" Error: {err}", | ||
exc_info=True, | ||
) | ||
raise EmailSendingError( | ||
f"Could not sent email invitation to {email} for portfolio {portfolio}. Portfolio invitation not saved." | ||
) from err | ||
|
@@ -319,11 +347,14 @@ def send_portfolio_update_emails_to_portfolio_admins(editor, portfolio, updated_ | |
"updated_info": updated_page, | ||
}, | ||
) | ||
except EmailSendingError: | ||
logger.warning( | ||
"Could not send email organization admin notification to %s " "for portfolio: %s", | ||
user.email, | ||
portfolio, | ||
except EmailSendingError as err: | ||
logger.error( | ||
"Failed to send portfolio org update notification email:\n" | ||
f" Requested User: {user}\n" | ||
f" Subject: portfolio_org_update_notification_subject.txt\n" | ||
f" To: {user.email}\n" | ||
f" Portfolio: {portfolio}\n" | ||
f" Error: {err}", | ||
exc_info=True, | ||
) | ||
all_emails_sent = False | ||
|
@@ -362,11 +393,14 @@ def send_portfolio_member_permission_update_email(requestor, permissions: UserPo | |
"date": date.today(), | ||
}, | ||
) | ||
except EmailSendingError: | ||
logger.warning( | ||
"Could not send email organization member update notification to %s " "for portfolio: %s", | ||
permissions.user.email, | ||
permissions.portfolio.organization_name, | ||
except EmailSendingError as err: | ||
logger.error( | ||
"Failed to send organization member update notification email:\n" | ||
f" Requestor Email: {requestor_email}\n" | ||
f" Subject: portfolio_update_subject.txt\n" | ||
f" To: {permissions.user.email}\n" | ||
f" Portfolio: {permissions.portfolio}\n" | ||
f" Error: {err}", | ||
exc_info=True, | ||
) | ||
return False | ||
|
@@ -403,11 +437,14 @@ def send_portfolio_member_permission_remove_email(requestor, permissions: UserPo | |
"requestor_email": requestor_email, | ||
}, | ||
) | ||
except EmailSendingError: | ||
logger.warning( | ||
"Could not send email organization member removal notification to %s " "for portfolio: %s", | ||
permissions.user.email, | ||
permissions.portfolio.organization_name, | ||
except EmailSendingError as err: | ||
logger.error( | ||
"Failed to send portfolio member removal email:\n" | ||
f" Requestor Email: {requestor_email}\n" | ||
f" Subject: portfolio_removal_subject.txt\n" | ||
f" To: {permissions.user.email}\n" | ||
f" Portfolio: {permissions.portfolio}\n" | ||
f" Error: {err}", | ||
exc_info=True, | ||
) | ||
return False | ||
|
@@ -444,11 +481,13 @@ def send_portfolio_invitation_remove_email(requestor, invitation: PortfolioInvit | |
"requestor_email": requestor_email, | ||
}, | ||
) | ||
except EmailSendingError: | ||
logger.warning( | ||
"Could not send email organization member removal notification to %s " "for portfolio: %s", | ||
invitation.email, | ||
invitation.portfolio.organization_name, | ||
except EmailSendingError as err: | ||
logger.error( | ||
"Failed to send portfolio invitation removal email:\n" | ||
f" Subject: portfolio_removal_subject.txt\n" | ||
f" To: {invitation.email}\n" | ||
f" Portfolio: {invitation.portfolio.organization_name}\n" | ||
f" Error: {err}", | ||
exc_info=True, | ||
) | ||
return False | ||
|
@@ -497,11 +536,15 @@ def _send_portfolio_admin_addition_emails_to_portfolio_admins(email: str, reques | |
"date": date.today(), | ||
}, | ||
) | ||
except EmailSendingError: | ||
logger.warning( | ||
"Could not send email organization admin notification to %s " "for portfolio: %s", | ||
user.email, | ||
portfolio.organization_name, | ||
except EmailSendingError as err: | ||
logger.error( | ||
"Failed to send portfolio admin addition notification email:\n" | ||
f" Requestor Email: {requestor_email}\n" | ||
f" Subject: portfolio_admin_addition_notification_subject.txt\n" | ||
f" To: {user.email}\n" | ||
f" Portfolio: {portfolio}\n" | ||
f" Portfolio Admin: {user}\n" | ||
f" Error: {err}", | ||
exc_info=True, | ||
) | ||
all_emails_sent = False | ||
|
@@ -550,11 +593,14 @@ def _send_portfolio_admin_removal_emails_to_portfolio_admins(email: str, request | |
"date": date.today(), | ||
}, | ||
) | ||
except EmailSendingError: | ||
logger.warning( | ||
"Could not send email organization admin notification to %s " "for portfolio: %s", | ||
user.email, | ||
portfolio.organization_name, | ||
except EmailSendingError as err: | ||
logger.error( | ||
"Failed to send portfolio admin removal notification email:\n" | ||
f" Requestor Email: {requestor_email}\n" | ||
f" Subject: portfolio_admin_removal_notification_subject.txt\n" | ||
f" To: {user.email}\n" | ||
f" Portfolio: {portfolio.organization_name}\n" | ||
f" Error: {err}", | ||
exc_info=True, | ||
) | ||
all_emails_sent = False | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got the following error when trying to test this script and prompting an error. I did this locally by automatically throwing an EmailSendingError in the try block's first line. For setup, I created a transition domain and tried running the script to prompt the email error but let me know if I did anything different from its expected use!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh my gosh great catch - I will edit the line to
f" Subject: transition_domain_invitation_subject.txt\n"
as we have the subject above, and the way I was trying to pull that info is incorrect