backoffice: display missing carddef/formdef in inspect template test (#59920)

This commit is contained in:
Frédéric Péters 2021-12-20 14:16:30 +01:00
parent 4fe9cd2f76
commit 4e6125718e
5 changed files with 46 additions and 20 deletions

View File

@ -604,3 +604,23 @@ def test_inspect_page_actions_traces(pub):
== 'http://example.net/backoffice/workflows/2/status/just_submitted/items/_notify_new_receiver_email/'
)
assert action_links[-1] == 'http://example.net/backoffice/workflows/2/global-actions/1/items/1/'
def test_inspect_page_missing_carddef_error(pub):
create_user(pub, is_admin=True)
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = []
formdef.store()
formdata = formdef.data_class()()
formdata.just_created()
formdata.store()
resp = login(get_app(pub)).get('%sinspect' % formdata.get_url(backoffice=True), status=200)
resp.form['test_mode'] = 'template'
resp.form['template'] = '{{ cards|objects:"XXX" }}'
resp = resp.form.submit()
assert 'Failed to evaluate template' in resp.text
assert ' such card model: XXX' in resp.text

View File

@ -3051,9 +3051,24 @@ class FormBackOfficeStatusPage(FormStatusPage):
form.add_submit('submit', _('Evaluate'))
return form
def get_inspect_error_message(self, exception):
if hasattr(exception, 'get_error_message'):
# dedicated message
return htmltext('<p>%s</p>') % exception.get_error_message()
# generic exception
try:
error_message = htmltext('<code>%s: %s</code>') % (
exception.__class__.__name__,
str(exception),
)
except UnicodeEncodeError:
error_message = htmltext('<code>%s</code>') % repr(exception)
return htmltext('<p>%s %s</p>') % (_('Error message:'), error_message)
def test_tool_result(self):
form = self.test_tools_form()
r = TemplateIO(html=True)
get_request().inspect_mode = True
if form.is_submitted() and not form.has_errors():
# show test result
test_mode = form.get_widget('test_mode').parse()
@ -3068,14 +3083,7 @@ class FormBackOfficeStatusPage(FormStatusPage):
except Exception as exception:
r += htmltext('<div class="errornotice">')
r += htmltext('<p>%s</p>') % _('Failed to evaluate condition')
try:
r += htmltext('<p>%s <code>%s: %s</code></p>') % (
_('Error message:'),
exception.__class__.__name__,
str(exception),
)
except UnicodeEncodeError:
r += htmltext('<p>%s <code>%s</code></p>') % (_('Error message:'), repr(exception))
r += self.get_inspect_error_message(exception)
r += htmltext('</div>')
else:
r += htmltext('<div class="test-tool-result infonotice">')
@ -3103,11 +3111,8 @@ class FormBackOfficeStatusPage(FormStatusPage):
except Exception as exception:
r += htmltext('<div class="errornotice">')
r += htmltext('<p>%s</p>') % _('Failed to evaluate template')
r += htmltext('<p>%s <code>%s: %s</code></p>') % (
_('Error message:'),
exception.__class__.__name__,
str(exception),
)
r += self.get_inspect_error_message(exception)
r += htmltext('</div>')
else:
r += htmltext('<div class="test-tool-result infonotice">')
r += htmltext('<h3>%s</h3>') % _('Template rendering:')
@ -3135,11 +3140,7 @@ class FormBackOfficeStatusPage(FormStatusPage):
except Exception as exception:
r += htmltext('<div class="errornotice">')
r += htmltext('<p>%s</p>') % _('Failed to evaluate HTML template')
r += htmltext('<p>%s <code>%s: %s</code></p>') % (
_('Error message:'),
exception.__class__.__name__,
str(exception),
)
r += self.get_inspect_error_message(exception)
r += htmltext('</div>')
else:
r += htmltext('<div class="test-tool-result infonotice">')

View File

@ -35,7 +35,7 @@ if not hasattr(types, 'ClassType'):
class CardDefDoesNotExist(FormDefDoesNotExist):
pass
error_message = _('No such card model: %s')
class CardDef(FormDef):

View File

@ -93,7 +93,10 @@ class FormdefImportRecoverableError(FormdefImportError):
class FormDefDoesNotExist(AttributeError):
pass
error_message = _('No such form: %s')
def get_error_message(self):
return self.error_message % self
class FormField:

View File

@ -517,6 +517,8 @@ class Template:
raise TemplateError(_('failure to render Django template: %s'), e)
return self.value
except (FormDefDoesNotExist, CardDefDoesNotExist) as e:
if get_request() and getattr(get_request(), 'inspect_mode', False):
raise
get_publisher().record_error(exception=e, notify=True)
return self.value
rendered = str(rendered)