wscall: record more informations in JournalWsCallErrorPart (#49538)

This commit is contained in:
Thomas NOËL 2020-12-16 15:42:09 +01:00
parent 83f8b5b4b2
commit f790982b96
1 changed files with 58 additions and 23 deletions

View File

@ -36,19 +36,37 @@ from wcs.workflows import (WorkflowStatusItem, register_item_class,
from wcs.wscalls import call_webservice, get_app_error_code
class JournalWsCallErrorPart: #pylint: disable=C1001
content = None
data = None
class JournalWsCallErrorPart:
summary = None
label = None
data = None
json_data = None
url = None
method = None
payload = None
def __init__(self, summary, label=None, data=None):
def __init__(self, summary, label=None, data=None, url=None, method=None, payload=None):
self.summary = summary
self.label = label
if data:
self.data = data[:10000] # beware of huge responses
self.data = data[:10000] # beware of huge responses
try:
json_data = json_loads(data)
# only retains meaningful values
self.json_data = {
'err': json_data.get('err'),
'err_class': json_data.get('err_class'),
'err_desc': json_data.get('err_desc'),
'reason': json_data.get('reason'),
}
except ValueError:
self.json_data = None
self.url = url
self.method = method
self.payload = payload
def view(self):
if not (get_request() and get_request().get_path().startswith('/backoffice/')):
if not (get_request() and get_request().is_in_backoffice()):
return ''
r = TemplateIO(html=True)
r += htmltext('<div class="ws-error">')
@ -58,26 +76,43 @@ class JournalWsCallErrorPart: #pylint: disable=C1001
else:
r += _('Error during webservice call')
r += htmltext('</h4>')
if self.url:
r += htmltext('<div>')
r += htmltext('<p>%s %s %s</p>\n') % (_('Request:'), self.method, self.url)
if self.payload:
r += htmltext('<p>%s</p>') % _('Payload:')
r += htmltext('<pre>%s</pre>') % self.payload
r += htmltext('</div>')
r += htmltext('<div>')
r += htmltext('<p>%s</p>\n') % self.summary
if self.data:
r += htmltext('<p>%s %s</p>\n') % (_('Response summary'), self.summary)
if not self.json_data and self.data:
# non-JSON response, or legacy JournalWsCallErrorPart without
# self.json_data: try to read self.data
try:
json_data = json_loads(self.data)
self.json_data = json_loads(self.data)
except ValueError:
pass
else:
labels = {
'err': _('Error Code'),
'err_class': _('Error Class'),
'err_desc': _('Error Description'),
'reason': _('Reason')
}
r += htmltext('<ul>')
for attr in ('err', 'err_class', 'err_desc', 'reason'):
if attr in json_data:
r += htmltext('<li>%s: %s</li>\n' ) % (
labels.get(attr), json_data[attr])
r += htmltext('</ul>')
r += htmltext('<p>%s</p>') % _('Non-JSON response:')
r += htmltext('<pre>%r</pre>') % self.data
if self.json_data:
labels = {
'err': _('Error Code'),
'err_class': _('Error Class'),
'err_desc': _('Error Description'),
'reason': _('Reason')
}
r += htmltext('<ul>')
for attr in ('err', 'err_class', 'err_desc', 'reason'):
if attr in self.json_data:
r += htmltext('<li>%s: %s</li>\n' ) % (
labels.get(attr), self.json_data[attr])
else:
r += htmltext('<li>%s: <i>%s</i></li>\n' ) % (
labels.get(attr), _('absent'))
r += htmltext('</ul>')
r += htmltext('<p>%s</p>') % _('Complete response (first 10kB):')
r += htmltext('<pre>%r</pre>') % self.data
if not self.data:
r += htmltext('<p>%s</p>') % _('Empty response')
r += htmltext('</div>')
r += htmltext('</div>')
return r.getvalue()