Skip to content

Commit

Permalink
Revert "Added exception catching to send default email if template fi…
Browse files Browse the repository at this point in the history
…le raises any exception(24943)"
  • Loading branch information
ephraimbuddy committed Aug 19, 2022
1 parent e6f6fde commit c93c56c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 41 deletions.
9 changes: 2 additions & 7 deletions airflow/models/taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2329,13 +2329,8 @@ def get_email_subject_content(
def render(key: str, content: str) -> str:
if conf.has_option('email', key):
path = conf.get_mandatory_value('email', key)
try:
with open(path) as f:
content = f.read()
except FileNotFoundError:
self.log.warning(f"Could not find email template file '{path!r}'. Using defaults...")
except OSError:
self.log.exception(f"Error while using email template '{path!r}'. Using defaults...")
with open(path) as f:
content = f.read()
return render_template_to_string(jinja_env.from_string(content), jinja_context)

subject = render('subject_template', default_subject)
Expand Down
34 changes: 0 additions & 34 deletions tests/models/test_taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,40 +1555,6 @@ def test_email_alert_with_config(self, mock_send_email, dag_maker):
assert 'template: test_email_alert_with_config' == title
assert 'template: test_email_alert_with_config' == body

@patch('airflow.models.taskinstance.send_email')
def test_email_alert_with_filenotfound_config(self, mock_send_email, dag_maker):
with dag_maker(dag_id='test_failure_email'):
task = BashOperator(
task_id='test_email_alert_with_config',
bash_command='exit 1',
email='to',
)
ti = dag_maker.create_dagrun(execution_date=timezone.utcnow()).task_instances[0]
ti.task = task

# Run test when the template file is not found
opener = mock_open(read_data='template: {{ti.task_id}}')
opener.side_effect = FileNotFoundError
with patch('airflow.models.taskinstance.open', opener, create=True):
try:
ti.run()
except AirflowException:
pass

(email_error, title_error, body_error), _ = mock_send_email.call_args

# Rerun task without any error and no template file
try:
ti.run()
except AirflowException:
pass

(email_default, title_default, body_default), _ = mock_send_email.call_args

assert email_error == email_default == 'to'
assert title_default == title_error
assert body_default == body_error

@pytest.mark.parametrize("task_id", ["test_email_alert", "test_email_alert__1"])
@patch('airflow.models.taskinstance.send_email')
def test_failure_mapped_taskflow(self, mock_send_email, dag_maker, session, task_id):
Expand Down

0 comments on commit c93c56c

Please sign in to comment.