wscall: dont not fail on err: '0' (#73217)

This commit is contained in:
Lauréline Guérin 2023-01-19 16:10:34 +01:00 committed by Gitea
parent 7044972314
commit cf59e991b6
3 changed files with 27 additions and 10 deletions

View File

@ -215,7 +215,12 @@ def test_webservice_on_error(http_requests, emails, notify_on_errors, record_on_
'404',
'404-json',
'500',
'json-err0',
'json-err0int',
'json-err1',
'json-err1int',
'json-err1-with-desc',
'json-errstr',
'json-errheader1',
'json-errheaderstr',
]:
@ -225,7 +230,12 @@ def test_webservice_on_error(http_requests, emails, notify_on_errors, record_on_
'404': '404 Not Found',
'404-json': '404 Not Found (err not-found)',
'500': '500 Internal Server Error',
'json-err1': '(err_desc :()',
'json-err0': None,
'json-err0int': None,
'json-err1': '(err 1)',
'json-err1int': '(err 1)',
'json-err1-with-desc': '(err_desc :()',
'json-errstr': '(err bug)',
'json-errheader1': '(err 1)',
'json-errheaderstr': '(err bug)',
}
@ -233,16 +243,17 @@ def test_webservice_on_error(http_requests, emails, notify_on_errors, record_on_
wscall.store()
resp = get_app(pub).get('/foobar/')
assert 'Foo Bar ' in resp.text
if notify_on_errors:
msg = msg_mapping[url_part]
if notify_on_errors and msg is not None:
assert emails.count() == 1
assert emails.get_latest('subject') == '[ERROR] [WSCALL] %s' % msg_mapping[url_part]
assert emails.get_latest('subject') == '[ERROR] [WSCALL] %s' % msg
emails.empty()
else:
assert emails.count() == 0
if record_on_errors:
if record_on_errors and msg is not None:
assert pub.loggederror_class.count() == 1
logged_error = pub.loggederror_class.select()[0]
assert logged_error.summary == '[WSCALL] %s' % msg_mapping[url_part]
assert logged_error.summary == '[WSCALL] %s' % msg
pub.loggederror_class.wipe()
else:
assert pub.loggederror_class.count() == 0

View File

@ -346,9 +346,6 @@ class HttpRequestsMocking:
self.requests_mock.get('http://remote.example.net/404', status=404, body='page not found')
self.requests_mock.get('http://remote.example.net/404-json', status=404, json={'err': 'not-found'})
self.requests_mock.get('http://remote.example.net/500', status=500, body='internal server error')
self.requests_mock.get(
'http://remote.example.net/json-err1', json={'data': '', 'err': 1, 'err_desc': ':('}
)
self.requests_mock.get('http://remote.example.net/json', json={"foo": "bar"})
self.requests_mock.post('http://remote.example.net/json', json={"foo": "bar"})
self.requests_mock.delete('http://remote.example.net/json', json={"foo": "bar"})
@ -378,7 +375,13 @@ class HttpRequestsMocking:
headers={'x-error-code': '1'},
)
self.requests_mock.get('http://remote.example.net/json-err0', json={"data": "foo", "err": 0})
self.requests_mock.get('http://remote.example.net/json-err0int', json={"data": "foo", "err": "0"})
self.requests_mock.get('http://remote.example.net/json-err1', json={"data": "", "err": 1})
self.requests_mock.get('http://remote.example.net/json-err1int', json={"data": "", "err": "1"})
self.requests_mock.get(
'http://remote.example.net/json-err1-with-desc', json={'data': '', 'err': 1, 'err_desc': ':('}
)
self.requests_mock.get('http://remote.example.net/json-errstr', json={"data": "", "err": "bug"})
self.requests_mock.get(
'http://remote.example.net/json-list-err1', json={"data": [{"id": "a", "text": "b"}], "err": 1}
)

View File

@ -57,7 +57,10 @@ def get_app_error_code(response, data, response_type):
pass
else:
if isinstance(d, dict) and d.get('err'):
app_error_code = d['err']
try:
app_error_code = int(d['err'])
except ValueError:
app_error_code = d['err']
return app_error_code
@ -189,7 +192,7 @@ def call_webservice(
for key in ['err_desc', 'err_class']:
if json_data.get(key):
details.append('%s %s' % (key, json_data[key]))
if not details or app_error_code not in [1, '1']:
if not details or app_error_code != 1:
details.append('err %s' % app_error_code)
details = '(%s)' % ', '.join(details)
summary = '%s %s' % (summary, details) if summary else details