cti-python-stix2/stix2/exceptions.py

59 lines
1.8 KiB
Python
Raw Normal View History

class STIXError(Exception):
"""Base class for errors generated in the stix2 library."""
2017-04-18 21:42:59 +02:00
class InvalidValueError(STIXError, ValueError):
"""An invalid value was provided to a STIX object's __init__."""
def __init__(self, cls, prop_name, reason):
2017-04-18 21:42:59 +02:00
super(InvalidValueError, self).__init__()
self.cls = cls
self.prop_name = prop_name
self.reason = reason
def __str__(self):
msg = "Invalid value for {0.cls.__name__} '{0.prop_name}': {0.reason}"
return msg.format(self)
2017-04-18 21:41:18 +02:00
class MissingFieldsError(STIXError, ValueError):
"""Missing required field(s) when constructing STIX object."""
2017-04-18 21:41:18 +02:00
def __init__(self, cls, fields):
super(MissingFieldsError, self).__init__()
self.cls = cls
self.fields = sorted(list(fields))
def __str__(self):
msg = "Missing required field(s) for {0}: ({1})."
return msg.format(self.cls.__name__,
", ".join(x for x in self.fields))
class ExtraFieldsError(STIXError, TypeError):
"""Extra field(s) were provided when constructing STIX object."""
def __init__(self, cls, fields):
super(ExtraFieldsError, self).__init__()
self.cls = cls
self.fields = sorted(list(fields))
def __str__(self):
msg = "Unexpected field(s) for {0}: ({1})."
return msg.format(self.cls.__name__,
", ".join(x for x in self.fields))
class ImmutableError(STIXError, ValueError):
"""Attempted to modify an object after creation"""
def __init__(self):
super(ImmutableError, self).__init__("Cannot modify properties after creation.")
2017-05-02 20:06:42 +02:00
class VersioningError(STIXError, ValueError):
"""Execption while using the Versioning API"""
def __init__(self, msg):
2017-05-02 20:17:26 +02:00
super(VersioningError, self).__init__(msg)