Skip to content

Commit

Permalink
Return partial errors (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
DinaMeylakh authored Oct 30, 2022
1 parent 51a40e5 commit 33e4886
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
[PyPI History][1]
[1]: https://pypi.org/project/demisto-py/#history

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

## 3.2.2
* Re-added Python 3.8 support.
Expand Down
6 changes: 5 additions & 1 deletion demisto_client/demisto_api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ def deserialize(self, response, response_type):
except ValueError:
data = response.data

return self.__deserialize(data, response_type)
deserialized_response = self.__deserialize(data, response_type)
if isinstance(data, dict) and 'error' in data.keys():
error_message = data.get('error')
return {'response': deserialized_response, 'error': error_message}
return deserialized_response

def __deserialize(self, data, klass):
"""Deserializes dict, list, str into an object.
Expand Down
12 changes: 11 additions & 1 deletion gen-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,17 @@ sed -i "${INPLACE[@]}" -e 's/PRIMITIVE_TYPES = (float, bool, bytes, six.text_typ
PRIMITIVE_TYPES = (float, bool, bytes, six.text_type) + six.integer_types/' -e 's/self.last_response = response_data/if self.CACHE_LAST_RESPONSE:\
self.last_response = response_data/' demisto_client/demisto_api/api_client.py
# End fix import_classifier

# Fix return partial errors
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"`
start_line=$((select_line))
sed -i "${INPLACE[@]}" -e "${start_line}a\\
\ \ \ \ \ \ \ \ deserialized_response = self.__deserialize(data, response_type)\\
\ \ \ \ \ \ \ \ if isinstance(data, dict) and 'error' in data.keys():\\
\ \ \ \ \ \ \ \ \ \ \ \ error_message = data.get('error')\\
\ \ \ \ \ \ \ \ \ \ \ \ return {'response': deserialized_response, 'error': error_message}\\
\ \ \ \ \ \ \ \ return deserialized_response" demisto_client/demisto_api/api_client.py
sed -i "${INPLACE[@]}" -e "${start_line}d" demisto_client/demisto_api/api_client.py
# End fix return partial errors
# remove files not used
rm .travis.yml
rm git_push.sh
Expand Down
28 changes: 28 additions & 0 deletions tests/mocks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import demisto_client
from datetime import datetime
import json
import urllib3
import tempfile
from demisto_client.demisto_api import rest

responses = Responses('requests.packages.urllib3')

api_key = 'sample_api_key'
Expand Down Expand Up @@ -382,3 +386,27 @@ def run():
assert 1 == 1
run()
assert_reset()


def test_import_incidentfields(mocker):
"""
Given:
A path for a incidentfield.
When:
Importing incidentfields with partial errors.
Then:
Make sure the partial error is returned.
"""

api_instance = demisto_client.configure(base_url=host, api_key=api_key, debug=False)

raw_http_response = urllib3.response.HTTPResponse(body=b'{"incidentFields":[{"id":"evidence_description"}],'
b'"error":"Partial Error Description"}\n', status=200)
mocker.patch.object(rest.RESTClientObject, 'POST', return_value=raw_http_response)
with tempfile.NamedTemporaryFile() as tmp:
res = api_instance.import_incident_fields(tmp.name)
if isinstance(res, dict):
assert res.get('error') == 'Partial Error Description'
else:
assert hasattr(res, 'error')
assert res.error == 'Partial Error Description'

0 comments on commit 33e4886

Please sign in to comment.