Skip to content

Commit 33e4886

Browse files
authored
Return partial errors (#101)
1 parent 51a40e5 commit 33e4886

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
[PyPI History][1]
44
[1]: https://pypi.org/project/demisto-py/#history
55

6+
## 3.2.3
7+
* Fixed an issue where demisto-py did not return partial errors when importing incident fields.
68

79
## 3.2.2
810
* Re-added Python 3.8 support.

demisto_client/demisto_api/api_client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ def deserialize(self, response, response_type):
240240
except ValueError:
241241
data = response.data
242242

243-
return self.__deserialize(data, response_type)
243+
deserialized_response = self.__deserialize(data, response_type)
244+
if isinstance(data, dict) and 'error' in data.keys():
245+
error_message = data.get('error')
246+
return {'response': deserialized_response, 'error': error_message}
247+
return deserialized_response
244248

245249
def __deserialize(self, data, klass):
246250
"""Deserializes dict, list, str into an object.

gen-code.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,17 @@ sed -i "${INPLACE[@]}" -e 's/PRIMITIVE_TYPES = (float, bool, bytes, six.text_typ
117117
PRIMITIVE_TYPES = (float, bool, bytes, six.text_type) + six.integer_types/' -e 's/self.last_response = response_data/if self.CACHE_LAST_RESPONSE:\
118118
self.last_response = response_data/' demisto_client/demisto_api/api_client.py
119119
# End fix import_classifier
120-
120+
# Fix return partial errors
121+
select_line=`grep "return self.__deserialize(data, response_type)" demisto_client/demisto_api/api_client.py -n | cut -f1 -d: | tail -1 | tr -d "\\n"`
122+
start_line=$((select_line))
123+
sed -i "${INPLACE[@]}" -e "${start_line}a\\
124+
\ \ \ \ \ \ \ \ deserialized_response = self.__deserialize(data, response_type)\\
125+
\ \ \ \ \ \ \ \ if isinstance(data, dict) and 'error' in data.keys():\\
126+
\ \ \ \ \ \ \ \ \ \ \ \ error_message = data.get('error')\\
127+
\ \ \ \ \ \ \ \ \ \ \ \ return {'response': deserialized_response, 'error': error_message}\\
128+
\ \ \ \ \ \ \ \ return deserialized_response" demisto_client/demisto_api/api_client.py
129+
sed -i "${INPLACE[@]}" -e "${start_line}d" demisto_client/demisto_api/api_client.py
130+
# End fix return partial errors
121131
# remove files not used
122132
rm .travis.yml
123133
rm git_push.sh

tests/mocks_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import demisto_client
55
from datetime import datetime
66
import json
7+
import urllib3
8+
import tempfile
9+
from demisto_client.demisto_api import rest
10+
711
responses = Responses('requests.packages.urllib3')
812

913
api_key = 'sample_api_key'
@@ -382,3 +386,27 @@ def run():
382386
assert 1 == 1
383387
run()
384388
assert_reset()
389+
390+
391+
def test_import_incidentfields(mocker):
392+
"""
393+
Given:
394+
A path for a incidentfield.
395+
When:
396+
Importing incidentfields with partial errors.
397+
Then:
398+
Make sure the partial error is returned.
399+
"""
400+
401+
api_instance = demisto_client.configure(base_url=host, api_key=api_key, debug=False)
402+
403+
raw_http_response = urllib3.response.HTTPResponse(body=b'{"incidentFields":[{"id":"evidence_description"}],'
404+
b'"error":"Partial Error Description"}\n', status=200)
405+
mocker.patch.object(rest.RESTClientObject, 'POST', return_value=raw_http_response)
406+
with tempfile.NamedTemporaryFile() as tmp:
407+
res = api_instance.import_incident_fields(tmp.name)
408+
if isinstance(res, dict):
409+
assert res.get('error') == 'Partial Error Description'
410+
else:
411+
assert hasattr(res, 'error')
412+
assert res.error == 'Partial Error Description'

0 commit comments

Comments
 (0)