Skip to content

Commit

Permalink
Merge pull request #4598 from broadinstitute/email-error-formatting
Browse files Browse the repository at this point in the history
handle email error log issue
  • Loading branch information
hanars authored Jan 17, 2025
2 parents 9fa33e7 + cd8406c commit 8138d4e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
3 changes: 1 addition & 2 deletions seqr/utils/communication_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ def send_project_notification(project, notification, subject, email_template=Non
email_body=BASE_EMAIL_TEMPLATE.format(email),
to=list(users.values_list('email', flat=True)),
subject=subject,
process_message=_set_bulk_notification_stream,
)
try:
send_html_email(**email_kwargs)
send_html_email(**email_kwargs, process_message=_set_bulk_notification_stream)
except Exception as e:
logger.error(f'Error sending project email for {project.guid}: {e}', extra={'detail': email_kwargs})

Expand Down
16 changes: 10 additions & 6 deletions seqr/views/apis/dataset_api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@
class DatasetAPITest(object):

@mock.patch('seqr.models.random.randint')
@mock.patch('seqr.utils.communication_utils.logger')
@mock.patch('seqr.utils.communication_utils.send_html_email')
@mock.patch('seqr.utils.communication_utils.BASE_URL', 'https://seqr.broadinstitute.org/')
@urllib3_responses.activate
def test_add_variants_dataset(self, mock_send_email, mock_logger, mock_random):
def test_add_variants_dataset(self, mock_send_email, mock_random):
url = reverse(add_variants_dataset_handler, args=[PROJECT_GUID])
self.check_data_manager_login(url)

Expand Down Expand Up @@ -247,6 +246,7 @@ def test_add_variants_dataset(self, mock_send_email, mock_logger, mock_random):
'sample_ids': {'buckets': [{'key': 'NA21234'}]}
}}, method=urllib3_responses.POST)

self.reset_logs()
mock_send_email.reset_mock()
mock_send_email.side_effect = Exception('Email server is not configured')
response = self.client.post(url, content_type='application/json', data=json.dumps({
Expand All @@ -259,11 +259,15 @@ def test_add_variants_dataset(self, mock_send_email, mock_logger, mock_random):
mock_send_email, sample_type='WES', count=1, project_guid=NON_ANALYST_PROJECT_GUID,
project_name='Non-Analyst Project', recipient='test_user_collaborator@test.com',
)
mock_logger.error.assert_called_with(
'Error sending project email for R0004_non_analyst_project: Email server is not configured', extra={'detail': {
'email_body': mock.ANY, 'process_message': mock.ANY,
self.assert_json_logs(user=None, offset=6, expected=[(
'Error sending project email for R0004_non_analyst_project: Email server is not configured', {'detail': {
'email_body': mock.ANY,
'subject': 'New data available in seqr', 'to': ['test_user_collaborator@test.com'],
}})
},
'@type': 'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent',
'severity': 'ERROR',
})
])

def _assert_expected_notification(self, mock_send_email, sample_type, count, email_content=None,
project_guid=PROJECT_GUID, project_name='1kg project nåme with uniçøde',
Expand Down
8 changes: 6 additions & 2 deletions seqr/views/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,19 @@ def reset_logs(self):
self._log_stream.truncate(0)
self._log_stream.seek(0)

def assert_json_logs(self, user, expected):
def assert_json_logs(self, user, expected, offset=0):
logs = self._log_stream.getvalue().split('\n')
if offset:
logs = logs[offset:]
for i, (message, extra) in enumerate(expected):
extra = extra or {}
validate = extra.pop('validate', None)
log_value = json.loads(logs[i])
expected_log = {
'timestamp': mock.ANY, 'severity': 'INFO', 'user': user.email, **extra,
'timestamp': mock.ANY, 'severity': 'INFO', **extra,
}
if user:
expected_log['user'] = user.email
if message is not None:
expected_log['message'] = message
self.assertDictEqual(log_value, expected_log)
Expand Down

0 comments on commit 8138d4e

Please sign in to comment.