Description
When working on #4121 i found another bug in the code.
Steps to reproduce
- Install email pack (or any other pack)
st2 pack install email
- Create a config file
/opt/stackstorm/packs/email.yaml
with an invalid jinja expression in it
---
smtp_accounts:
- name: me
password: "password"
port: 465
secure: true
server: "smtp.example.com"
smtp_auth: true
username: "user"
- name: you
password: "password"
port: 465
secure: true
server: "smtp.example.com"
smtp_auth: true
username: "user"
imap_accounts:
- name: me
download_attachments: true
folder: "INBOX"
password: "super_S3c4e3t!"
port: 993
secure: true
server: "imap.example.com"
username: "me@example.com"
- name: you
download_attachments: true
folder: "INBOX"
password: "topsecret!"
port: 993
secure: true
server: "imap.example.com"
username: "you@example.com"
max_attachment_size: 1024
attachment_datastore_ttl: 1800
sensor_smtp_listen_ip: "{{ some invalid jinja that won't render properly }}"
sensor_smtp_listen_port: 25
- Register the config
st2ctl reload --register-configs
- Run an action in that pack and you'll get the following error:
$ st2 run email.send_email email_from="nick.maludy@encore.tech" email_to=['nick.maludy@encore.tech'] subject="test subj" message="test message" account="me"
.
id: 5af3b8f4a814c021281db64c
status: failed
parameters:
account: me
email_from: nick.maludy@encore.tech
email_to:
- '[nick.maludy@encore.tech]'
message: test message
subject: test subj
result:
error: __init__() takes at least 3 arguments (2 given)
traceback: " File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2actions/worker.py", line 160, in _run_action
result = self.container.dispatch(liveaction_db)
File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2actions/container/base.py", line 63, in dispatch
runner = self._get_runner(runnertype_db, action_db, liveaction_db)
File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2actions/container/base.py", line 337, in _get_runner
config = config_loader.get_config()
File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/util/config_loader.py", line 83, in get_config
config_db=config_db)
File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/util/config_loader.py", line 95, in _get_values_for_config
config = self._assign_dynamic_config_values(schema=schema_values, config=config)
File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/util/config_loader.py", line 131, in _assign_dynamic_config_values
config_schema_item=schema_item)
File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/util/config_loader.py", line 192, in _get_datastore_value_for_expression
raise exc_class(msg)
"
Expected Behavior
The code https://github.com/StackStorm/st2/blob/master/st2common/st2common/util/config_loader.py#L192 should have re-thrown the underlying Jinja exception (jinja2.exceptions.TemplateSyntaxError
). However, this exception signature takes a different number of parameters than what's being passed in: https://github.com/pallets/jinja/blob/master/jinja2/exceptions.py#L84
Since the code is only passing in a message and leaving out the lineno
argument, a different exception is thrown for an invalid call. This defeats the purpose of catching/re-throwing in the first place.
Thoughts on a fix
I don't see many "good" ways to fix this that would be future proof while continuing to throw the underlying exception. My only thought would be to wrap up the exception details, add that to the message and throw a new exception type that we know and can control the parameters of.