From 2b4c5bf264d15ba5dfbc1151fe80a34de00c0a91 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 15 May 2018 17:42:19 -0400 Subject: [PATCH] handling and modifying exception messages in a manner acceptable by all python versions --- stix2/datastore/taxii.py | 35 ++++++++++++++++++++---------- stix2/test/test_datastore_taxii.py | 4 ++-- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/stix2/datastore/taxii.py b/stix2/datastore/taxii.py index 776baf7..4a047a8 100644 --- a/stix2/datastore/taxii.py +++ b/stix2/datastore/taxii.py @@ -69,9 +69,13 @@ class TAXIICollectionSink(DataSink): " to the underlying linked Collection resource") except (HTTPError, ValidationError) as e: - e.message = ("The underlying TAXII Collection resource defined in the supplied TAXII" - " Collection object provided could not be reached. TAXII Collection Error: " - + e.message) + added_context = ("The underlying TAXII Collection resource defined in the supplied TAXII" + " Collection object provided could not be reached.") + if not e.args: + e.args = (added_context,) + else: + e.args = (added_context,) + e.args + raise self.allow_custom = allow_custom @@ -144,9 +148,13 @@ class TAXIICollectionSource(DataSource): " to the underlying linked Collection resource") except (HTTPError, ValidationError) as e: - e.message = ("The underlying TAXII Collection resource defined in the supplied TAXII" - " Collection object provided could not be reached. TAXII Collection Error: " - + e.message) + added_context = ("The underlying TAXII Collection resource defined in the supplied TAXII" + " Collection object provided could not be reached.") + if not e.args: + e.args = (added_context,) + else: + e.args = (added_context,) + e.args + raise self.allow_custom = allow_custom @@ -275,12 +283,17 @@ class TAXIICollectionSource(DataSource): query.remove(taxii_filters) 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 err.response.status_code == 404: - err.message = ("The requested STIX objects for the TAXII Collection resource defined in" - " the supplied TAXII Collection object is either not found or access is" - " denied. Received error: " + err.message) + if e.response.status_code == 404: + 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" + " denied. Received error: ") + if not e.args: + e.args = (added_context,) + else: + e.args = (added_context,) + e.args + raise # parse python STIX objects from the STIX object dicts diff --git a/stix2/test/test_datastore_taxii.py b/stix2/test/test_datastore_taxii.py index af8fecd..a153ce8 100644 --- a/stix2/test/test_datastore_taxii.py +++ b/stix2/test/test_datastore_taxii.py @@ -361,7 +361,7 @@ def test_all_versions_404(collection): ds = TAXIICollectionStore(collection) with pytest.raises(HTTPError) as excinfo: 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) @@ -373,5 +373,5 @@ def test_query_404(collection): with pytest.raises(HTTPError) as excinfo: 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)