Rename exception class.
parent
a7805c4ac0
commit
32ff00559e
|
@ -4,7 +4,7 @@ import collections
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from .exceptions import STIXValueError, MissingFieldsError
|
from .exceptions import InvalidValueError, MissingFieldsError
|
||||||
from .utils import format_datetime, get_timestamp, NOW
|
from .utils import format_datetime, get_timestamp, NOW
|
||||||
|
|
||||||
__all__ = ['STIXJSONEncoder', '_STIXBase']
|
__all__ = ['STIXJSONEncoder', '_STIXBase']
|
||||||
|
@ -42,7 +42,7 @@ class _STIXBase(collections.Mapping):
|
||||||
try:
|
try:
|
||||||
kwargs[prop_name] = prop.validate(kwargs[prop_name])
|
kwargs[prop_name] = prop.validate(kwargs[prop_name])
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
raise STIXValueError(self.__class__, prop_name, reason=str(exc))
|
raise InvalidValueError(self.__class__, prop_name, reason=str(exc))
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
cls = self.__class__
|
cls = self.__class__
|
||||||
|
|
|
@ -2,11 +2,11 @@ class STIXError(Exception):
|
||||||
"""Base class for errors generated in the stix2 library."""
|
"""Base class for errors generated in the stix2 library."""
|
||||||
|
|
||||||
|
|
||||||
class STIXValueError(STIXError, ValueError):
|
class InvalidValueError(STIXError, ValueError):
|
||||||
"""An invalid value was provided to a STIX object's __init__."""
|
"""An invalid value was provided to a STIX object's __init__."""
|
||||||
|
|
||||||
def __init__(self, cls, prop_name, reason):
|
def __init__(self, cls, prop_name, reason):
|
||||||
super(STIXValueError, self).__init__()
|
super(InvalidValueError, self).__init__()
|
||||||
self.cls = cls
|
self.cls = cls
|
||||||
self.prop_name = prop_name
|
self.prop_name = prop_name
|
||||||
self.reason = reason
|
self.reason = reason
|
||||||
|
|
|
@ -51,7 +51,7 @@ def test_empty_bundle():
|
||||||
|
|
||||||
|
|
||||||
def test_bundle_with_wrong_type():
|
def test_bundle_with_wrong_type():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Bundle(type="not-a-bundle")
|
stix2.Bundle(type="not-a-bundle")
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Bundle
|
assert excinfo.value.cls == stix2.Bundle
|
||||||
|
@ -61,7 +61,7 @@ def test_bundle_with_wrong_type():
|
||||||
|
|
||||||
|
|
||||||
def test_bundle_id_must_start_with_bundle():
|
def test_bundle_id_must_start_with_bundle():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Bundle(id='my-prefix--')
|
stix2.Bundle(id='my-prefix--')
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Bundle
|
assert excinfo.value.cls == stix2.Bundle
|
||||||
|
@ -71,7 +71,7 @@ def test_bundle_id_must_start_with_bundle():
|
||||||
|
|
||||||
|
|
||||||
def test_bundle_with_wrong_spec_version():
|
def test_bundle_with_wrong_spec_version():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Bundle(spec_version="1.2")
|
stix2.Bundle(spec_version="1.2")
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Bundle
|
assert excinfo.value.cls == stix2.Bundle
|
||||||
|
|
|
@ -67,7 +67,7 @@ def test_indicator_autogenerated_fields(indicator):
|
||||||
|
|
||||||
|
|
||||||
def test_indicator_type_must_be_indicator():
|
def test_indicator_type_must_be_indicator():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Indicator(type='xxx', **INDICATOR_KWARGS)
|
stix2.Indicator(type='xxx', **INDICATOR_KWARGS)
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Indicator
|
assert excinfo.value.cls == stix2.Indicator
|
||||||
|
@ -77,7 +77,7 @@ def test_indicator_type_must_be_indicator():
|
||||||
|
|
||||||
|
|
||||||
def test_indicator_id_must_start_with_indicator():
|
def test_indicator_id_must_start_with_indicator():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Indicator(id='my-prefix--', **INDICATOR_KWARGS)
|
stix2.Indicator(id='my-prefix--', **INDICATOR_KWARGS)
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Indicator
|
assert excinfo.value.cls == stix2.Indicator
|
||||||
|
@ -105,7 +105,7 @@ def test_indicator_required_field_pattern():
|
||||||
|
|
||||||
|
|
||||||
def test_indicator_created_ref_invalid_format():
|
def test_indicator_created_ref_invalid_format():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Indicator(created_by_ref='myprefix--12345678', **INDICATOR_KWARGS)
|
stix2.Indicator(created_by_ref='myprefix--12345678', **INDICATOR_KWARGS)
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Indicator
|
assert excinfo.value.cls == stix2.Indicator
|
||||||
|
@ -115,7 +115,7 @@ def test_indicator_created_ref_invalid_format():
|
||||||
|
|
||||||
|
|
||||||
def test_indicator_revoked_invalid():
|
def test_indicator_revoked_invalid():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Indicator(revoked='false', **INDICATOR_KWARGS)
|
stix2.Indicator(revoked='false', **INDICATOR_KWARGS)
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Indicator
|
assert excinfo.value.cls == stix2.Indicator
|
||||||
|
|
|
@ -51,7 +51,7 @@ def test_malware_autogenerated_fields(malware):
|
||||||
|
|
||||||
|
|
||||||
def test_malware_type_must_be_malware():
|
def test_malware_type_must_be_malware():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Malware(type='xxx', **MALWARE_KWARGS)
|
stix2.Malware(type='xxx', **MALWARE_KWARGS)
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Malware
|
assert excinfo.value.cls == stix2.Malware
|
||||||
|
@ -61,7 +61,7 @@ def test_malware_type_must_be_malware():
|
||||||
|
|
||||||
|
|
||||||
def test_malware_id_must_start_with_malware():
|
def test_malware_id_must_start_with_malware():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Malware(id='my-prefix--', **MALWARE_KWARGS)
|
stix2.Malware(id='my-prefix--', **MALWARE_KWARGS)
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Malware
|
assert excinfo.value.cls == stix2.Malware
|
||||||
|
|
|
@ -87,7 +87,7 @@ def test_granular_example():
|
||||||
|
|
||||||
|
|
||||||
def test_granular_example_with_bad_selector():
|
def test_granular_example_with_bad_selector():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.GranularMarking(
|
stix2.GranularMarking(
|
||||||
marking_ref="marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
marking_ref="marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
||||||
selectors=["abc[0]"] # missing "."
|
selectors=["abc[0]"] # missing "."
|
||||||
|
|
|
@ -54,7 +54,7 @@ def test_relationship_autogenerated_fields(relationship):
|
||||||
|
|
||||||
|
|
||||||
def test_relationship_type_must_be_relationship():
|
def test_relationship_type_must_be_relationship():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Relationship(type='xxx', **RELATIONSHIP_KWARGS)
|
stix2.Relationship(type='xxx', **RELATIONSHIP_KWARGS)
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Relationship
|
assert excinfo.value.cls == stix2.Relationship
|
||||||
|
@ -64,7 +64,7 @@ def test_relationship_type_must_be_relationship():
|
||||||
|
|
||||||
|
|
||||||
def test_relationship_id_must_start_with_relationship():
|
def test_relationship_id_must_start_with_relationship():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Relationship(id='my-prefix--', **RELATIONSHIP_KWARGS)
|
stix2.Relationship(id='my-prefix--', **RELATIONSHIP_KWARGS)
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Relationship
|
assert excinfo.value.cls == stix2.Relationship
|
||||||
|
|
|
@ -63,7 +63,7 @@ def test_report_example_objects_in_object_refs():
|
||||||
|
|
||||||
|
|
||||||
def test_report_example_objects_in_object_refs_with_bad_id():
|
def test_report_example_objects_in_object_refs_with_bad_id():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Report(
|
stix2.Report(
|
||||||
id="report--84e4d88f-44ea-4bcd-bbf3-b2c1c320bcb3",
|
id="report--84e4d88f-44ea-4bcd-bbf3-b2c1c320bcb3",
|
||||||
created_by_ref="identity--a463ffb3-1bd9-4d94-b02d-74e4f1658283",
|
created_by_ref="identity--a463ffb3-1bd9-4d94-b02d-74e4f1658283",
|
||||||
|
|
|
@ -48,7 +48,7 @@ def test_sighting_all_required_fields():
|
||||||
def test_sighting_bad_where_sighted_refs():
|
def test_sighting_bad_where_sighted_refs():
|
||||||
now = dt.datetime(2016, 4, 6, 20, 6, 37, tzinfo=pytz.utc)
|
now = dt.datetime(2016, 4, 6, 20, 6, 37, tzinfo=pytz.utc)
|
||||||
|
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Sighting(
|
stix2.Sighting(
|
||||||
type='sighting',
|
type='sighting',
|
||||||
id=SIGHTING_ID,
|
id=SIGHTING_ID,
|
||||||
|
@ -65,7 +65,7 @@ def test_sighting_bad_where_sighted_refs():
|
||||||
|
|
||||||
|
|
||||||
def test_sighting_type_must_be_sightings():
|
def test_sighting_type_must_be_sightings():
|
||||||
with pytest.raises(stix2.exceptions.STIXValueError) as excinfo:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.Sighting(type='xxx', **SIGHTING_KWARGS)
|
stix2.Sighting(type='xxx', **SIGHTING_KWARGS)
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.Sighting
|
assert excinfo.value.cls == stix2.Sighting
|
||||||
|
|
Loading…
Reference in New Issue