handling and modifying exception messages in a manner acceptable by all python versions

stix2.0
= 2018-05-15 17:42:19 -04:00
parent 0d3f80f2fe
commit 2b4c5bf264
2 changed files with 26 additions and 13 deletions

View File

@ -69,9 +69,13 @@ class TAXIICollectionSink(DataSink):
" to the underlying linked Collection resource") " to the underlying linked Collection resource")
except (HTTPError, ValidationError) as e: except (HTTPError, ValidationError) as e:
e.message = ("The underlying TAXII Collection resource defined in the supplied TAXII" added_context = ("The underlying TAXII Collection resource defined in the supplied TAXII"
" Collection object provided could not be reached. TAXII Collection Error: " " Collection object provided could not be reached.")
+ e.message) if not e.args:
e.args = (added_context,)
else:
e.args = (added_context,) + e.args
raise raise
self.allow_custom = allow_custom self.allow_custom = allow_custom
@ -144,9 +148,13 @@ class TAXIICollectionSource(DataSource):
" to the underlying linked Collection resource") " to the underlying linked Collection resource")
except (HTTPError, ValidationError) as e: except (HTTPError, ValidationError) as e:
e.message = ("The underlying TAXII Collection resource defined in the supplied TAXII" added_context = ("The underlying TAXII Collection resource defined in the supplied TAXII"
" Collection object provided could not be reached. TAXII Collection Error: " " Collection object provided could not be reached.")
+ e.message) if not e.args:
e.args = (added_context,)
else:
e.args = (added_context,) + e.args
raise raise
self.allow_custom = allow_custom self.allow_custom = allow_custom
@ -275,12 +283,17 @@ class TAXIICollectionSource(DataSource):
query.remove(taxii_filters) query.remove(taxii_filters)
all_data = list(apply_common_filters(all_data, query)) all_data = list(apply_common_filters(all_data, query))
except HTTPError as err: except HTTPError as e:
# if resources not found or access is denied from TAXII server, return empty list # if resources not found or access is denied from TAXII server, return empty list
if err.response.status_code == 404: if e.response.status_code == 404:
err.message = ("The requested STIX objects for the TAXII Collection resource defined in" added_context = ("The requested STIX objects for the TAXII Collection resource defined in"
" the supplied TAXII Collection object is either not found or access is" " the supplied TAXII Collection object is either not found or access is"
" denied. Received error: " + err.message) " denied. Received error: ")
if not e.args:
e.args = (added_context,)
else:
e.args = (added_context,) + e.args
raise raise
# parse python STIX objects from the STIX object dicts # parse python STIX objects from the STIX object dicts

View File

@ -361,7 +361,7 @@ def test_all_versions_404(collection):
ds = TAXIICollectionStore(collection) ds = TAXIICollectionStore(collection)
with pytest.raises(HTTPError) as excinfo: with pytest.raises(HTTPError) as excinfo:
ds.all_versions("indicator--1") ds.all_versions("indicator--1")
assert "is either not found or access is denied" in str(excinfo.value.message) assert "is either not found or access is denied" in str(excinfo.value)
assert "404" in str(excinfo.value) assert "404" in str(excinfo.value)
@ -373,5 +373,5 @@ def test_query_404(collection):
with pytest.raises(HTTPError) as excinfo: with pytest.raises(HTTPError) as excinfo:
ds.query(query=query) ds.query(query=query)
assert "is either not found or access is denied" in str(excinfo.value.message) assert "is either not found or access is denied" in str(excinfo.value)
assert "404" in str(excinfo.value) assert "404" in str(excinfo.value)