2017-04-18 21:19:16 +02:00
|
|
|
class STIXError(Exception):
|
|
|
|
"""Base class for errors generated in the stix2 library."""
|
|
|
|
|
|
|
|
|
2017-04-18 21:42:59 +02:00
|
|
|
class InvalidValueError(STIXError, ValueError):
|
2017-04-18 21:19:16 +02:00
|
|
|
"""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__()
|
2017-04-18 21:19:16 +02:00
|
|
|
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):
|
2017-04-18 21:56:16 +02:00
|
|
|
"""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):
|
2017-05-04 22:34:08 +02:00
|
|
|
msg = "No values for required field(s) for {0}: ({1})."
|
2017-04-18 21:41:18 +02:00
|
|
|
return msg.format(self.cls.__name__,
|
|
|
|
", ".join(x for x in self.fields))
|
2017-04-18 21:56:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
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))
|
2017-04-18 22:05:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
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-03 20:10:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DictionaryKeyError(STIXError, ValueError):
|
|
|
|
"""Dictionary key does not conform to the correct format."""
|
|
|
|
|
|
|
|
def __init__(self, key, reason):
|
|
|
|
super(DictionaryKeyError, self).__init__()
|
|
|
|
self.key = key
|
|
|
|
self.reason = reason
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
msg = "Invliad dictionary key {0.key}: ({0.reason})."
|
|
|
|
return msg.format(self)
|
2017-05-05 18:32:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
class InvalidObjRefError(STIXError, ValueError):
|
|
|
|
"""A STIX Cyber Observable Object contains an invalid object reference."""
|
|
|
|
|
|
|
|
def __init__(self, cls, prop_name, reason):
|
|
|
|
super(InvalidObjRefError, self).__init__()
|
|
|
|
self.cls = cls
|
|
|
|
self.prop_name = prop_name
|
|
|
|
self.reason = reason
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
msg = "Invalid object reference for '{0.cls.__name__}:{0.prop_name}': {0.reason}"
|
|
|
|
return msg.format(self)
|
2017-05-08 17:11:56 +02:00
|
|
|
|
|
|
|
|
2017-05-03 18:14:09 +02:00
|
|
|
class UnmodifiablePropertyError(STIXError, ValueError):
|
|
|
|
"""Attempted to modify an unmodifiable property of object when creating a new version"""
|
2017-05-02 20:06:42 +02:00
|
|
|
|
2017-05-03 18:14:09 +02:00
|
|
|
def __init__(self, unchangable_properties):
|
|
|
|
super(UnmodifiablePropertyError, self).__init__()
|
|
|
|
self.unchangable_properties = unchangable_properties
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
msg = "These properties cannot be changed when making a new version: {0}."
|
|
|
|
return msg.format(", ".join(self.unchangable_properties))
|
|
|
|
|
|
|
|
|
|
|
|
class RevokeError(STIXError, ValueError):
|
|
|
|
"""Attempted to an operation on a revoked object"""
|
|
|
|
|
|
|
|
def __init__(self, called_by):
|
|
|
|
super(RevokeError, self).__init__()
|
|
|
|
self.called_by = called_by
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self.called_by == "revoke":
|
|
|
|
return "Cannot revoke an already revoked object."
|
|
|
|
else:
|
|
|
|
return "Cannot create a new version of a revoked object."
|