Make Jinja evaluator catch and wrap all underlying exceptions

Change-Id: Ie45dcc51519a839a0983860711cba48fa4a112a1
Closes-Bug: 1642574
This commit is contained in:
Renat Akhmerov 2016-11-18 16:37:00 +07:00
parent 1ae33591fc
commit 764f5298dc
2 changed files with 39 additions and 2 deletions

View File

@ -83,9 +83,10 @@ class JinjaEvaluator(Evaluator):
# the value is accessed, not when it gets created. The simplest way
# to access it is to try and cast it to string.
str(result)
except jinja2.exceptions.UndefinedError as e:
except Exception as e:
raise exc.JinjaEvaluationException(
"Undefined error '%s'." % str(e)
"Can not evaluate Jinja expression [expression=%s, error=%s"
", data=%s]" % (expression, str(e), data_context)
)
LOG.debug("Jinja expression result: %s" % result)

View File

@ -622,3 +622,39 @@ class ErrorHandlingEngineTest(base.EngineTestCase):
self.assertEqual(states.ERROR, task_ex.state)
self.assertIsNotNone(task_ex.state_info)
self.assertEqual(states.ERROR, wf_ex.state)
def test_publish_bad_jinja(self):
wf_text = """---
version: '2.0'
wf:
type: direct
input:
- my_dict:
- id: 1
value: 11
tasks:
task1:
action: std.noop
publish:
problem_var: '{{ (_.my_dict|some_invalid_filter).id }}'
"""
wf_service.create_workflows(wf_text)
wf_ex = self.engine.start_workflow('wf', {})
self.await_workflow_error(wf_ex.id)
with db_api.transaction():
wf_ex = db_api.get_workflow_execution(wf_ex.id)
task_ex = wf_ex.task_executions[0]
action_ex = task_ex.action_executions[0]
self.assertEqual(states.SUCCESS, action_ex.state)
self.assertEqual(states.ERROR, task_ex.state)
self.assertIsNotNone(task_ex.state_info)
self.assertEqual(states.ERROR, wf_ex.state)