Normalize error messages

pull/3/head v2.2
Raphaël Vinot 2015-12-19 17:57:29 +01:00
parent a334055749
commit a7fc1ceb20
2 changed files with 33 additions and 3 deletions

View File

@ -1,3 +1,3 @@
__version__ = '2.1.1' __version__ = '2.2'
from .api import PyMISP, PyMISPError, NewEventError, NewAttributeError, MissingDependency, NoURL, NoKey from .api import PyMISP, PyMISPError, NewEventError, NewAttributeError, MissingDependency, NoURL, NoKey

View File

@ -142,6 +142,23 @@ class PyMISP(object):
'content-type': 'application/' + out}) 'content-type': 'application/' + out})
return session return session
def flatten_error_messages(self, response):
messages = []
if response.get('error'):
if isinstance(response['error'], list):
for e in response['errors']:
messages.append(e['error']['value'][0])
else:
messages.append(['error'])
elif response.get('errors'):
if isinstance(response['errors'], dict):
for where, errors in response['errors'].items():
for e in errors:
for type_e, msgs in e.items():
for m in msgs:
messages.append('Error in {}: {}'.format(where, m))
return messages
def _check_response(self, response): def _check_response(self, response):
if response.status_code >= 500: if response.status_code >= 500:
response.raise_for_status() response.raise_for_status()
@ -151,9 +168,22 @@ class PyMISP(object):
if self.debug: if self.debug:
print(response.text) print(response.text)
raise PyMISPError('Unknown error: {}'.format(response.text)) raise PyMISPError('Unknown error: {}'.format(response.text))
errors = []
if to_return.get('error'):
if not isinstance(to_return['error'], list):
errors.append(to_return['error'])
else:
errors += to_return['error']
if 400 <= response.status_code < 500: if 400 <= response.status_code < 500:
if to_return.get('error') is None: if to_return.get('error') is None and to_return.get('message'):
to_return['error'] = to_return.get('message') errors.append(to_return['message'])
else:
errors.append(basestring(response.status_code))
errors += self.flatten_error_messages(to_return)
if errors:
to_return['errors'] = errors
if self.debug: if self.debug:
print(json.dumps(to_return, indent=4)) print(json.dumps(to_return, indent=4))
return to_return return to_return