workflow: record LoggedError on dispatch error (#48944)

This commit is contained in:
Lauréline Guérin 2020-12-03 15:24:35 +01:00
parent acc2432fc8
commit d0de376c1b
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 35 additions and 10 deletions

View File

@ -455,6 +455,8 @@ def test_dispatch(pub):
def test_dispatch_auto(pub):
LoggedError.wipe()
formdef = FormDef()
formdef.name = 'baz'
formdef.fields = [
@ -513,11 +515,28 @@ def test_dispatch_auto(pub):
item.perform(formdata)
assert formdata.workflow_roles == {'_receiver': role2.id}
# unknown role
formdata.data = {'1': 'foo'}
formdata.workflow_roles = {}
item.variable = variable
item.rules = [
{'role_id': 'foobar', 'value': 'foo'},
]
pub.substitutions.reset()
pub.substitutions.feed(formdata)
item.perform(formdata)
assert not formdata.workflow_roles
assert LoggedError.count() == 1
error = LoggedError.select()[0]
assert error.tech_id == '%s-_default-error-in-dispatch-missing-role-foobar' % formdef.id
assert error.formdef_id == formdef.id
assert error.workflow_id == '_default'
assert error.summary == 'error in dispatch, missing role (foobar)'
assert error.occurences_count == 1
def test_dispatch_computed(pub, caplog):
pub.cfg['debug'] = {'logger': True}
pub.write_cfg()
pub.get_app_logger(force=True) # force new logger
def test_dispatch_computed(pub):
LoggedError.wipe()
formdef = FormDef()
formdef.name = 'baz'
@ -536,13 +555,13 @@ def test_dispatch_computed(pub, caplog):
formdata = formdef.data_class()()
item.role_key = '_receiver'
item.role_id = '="yyy"' # slug
item.role_id = '="yyy"' # slug
item.perform(formdata)
assert formdata.workflow_roles == {'_receiver': role.id}
formdata = formdef.data_class()()
item.role_key = '_receiver'
item.role_id = '="xxx"' # name
item.role_id = '="xxx"' # name
item.perform(formdata)
assert formdata.workflow_roles == {'_receiver': role.id}
@ -552,7 +571,13 @@ def test_dispatch_computed(pub, caplog):
item.role_id = '="foobar"'
item.perform(formdata)
assert not formdata.workflow_roles
assert caplog.records[-1].message == 'error in dispatch, missing role (="foobar")'
assert LoggedError.count() == 1
error = LoggedError.select()[0]
assert error.tech_id == '%s-_default-error-in-dispatch-missing-role-foobar' % formdef.id
assert error.formdef_id == formdef.id
assert error.workflow_id == '_default'
assert error.summary == 'error in dispatch, missing role (="foobar")'
assert error.occurences_count == 1
def test_roles(pub):

View File

@ -22,7 +22,6 @@ from quixote.html import htmltext
from ..qommon import _, N_
from ..qommon.form import *
from ..qommon.template import Template
from ..qommon import get_logger
from wcs.roles import Role, get_user_roles
from wcs.workflows import XmlSerialisable, WorkflowStatusItem, register_item_class, get_role_name
@ -189,6 +188,7 @@ class DispatchWorkflowStatusItem(WorkflowStatusItem):
return htmltext('<ul class="rules">%s</ul>') % htmltext('').join(result)
def perform(self, formdata):
from wcs.logged_errors import LoggedError
if not formdata.workflow_roles:
formdata.workflow_roles = {}
@ -199,7 +199,7 @@ class DispatchWorkflowStatusItem(WorkflowStatusItem):
return
new_role_id = self.get_computed_role_id(self.role_id)
if not new_role_id:
get_logger().error('error in dispatch, missing role (%s)' % self.role_id)
LoggedError.record(_('error in dispatch, missing role (%s)') % self.role_id, formdata=formdata)
elif self.dispatch_type == 'automatic':
if not (self.role_key and self.variable and self.rules):
return
@ -226,7 +226,7 @@ class DispatchWorkflowStatusItem(WorkflowStatusItem):
if new_role_id:
if not Role.has_key(new_role_id):
get_logger().error('error in dispatch, missing role %s' % new_role_id)
LoggedError.record(_('error in dispatch, missing role (%s)') % new_role_id, formdata=formdata)
else:
formdata.workflow_roles[self.role_key] = str(new_role_id)
formdata.store()