From 49f58ff513823427b62fcbdcf0ddfcca223df697 Mon Sep 17 00:00:00 2001 From: Emmanuelle Vargas-Gonzalez Date: Thu, 1 Jun 2017 12:43:06 -0400 Subject: [PATCH] Make Immutable error more descriptive. Also fixes #13 --- stix2/base.py | 2 +- stix2/exceptions.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/stix2/base.py b/stix2/base.py index 7d2bf24..f7ab932 100644 --- a/stix2/base.py +++ b/stix2/base.py @@ -128,7 +128,7 @@ class _STIXBase(collections.Mapping): def __setattr__(self, name, value): if name != '_inner' and not name.startswith("_STIXBase__"): - raise ImmutableError + raise ImmutableError(self.__class__, name) super(_STIXBase, self).__setattr__(name, value) def __str__(self): diff --git a/stix2/exceptions.py b/stix2/exceptions.py index 5cbaae9..1165ecf 100644 --- a/stix2/exceptions.py +++ b/stix2/exceptions.py @@ -47,8 +47,14 @@ class ExtraPropertiesError(STIXError, TypeError): class ImmutableError(STIXError, ValueError): """Attempted to modify an object after creation""" - def __init__(self): - super(ImmutableError, self).__init__("Cannot modify properties after creation.") + def __init__(self, cls, key): + super(ImmutableError, self).__init__() + self.cls = cls + self.key = key + + def __str__(self): + msg = "Cannot modify '{0.key}' property in {0.cls} after creation." + return msg.format(self) class DictionaryKeyError(STIXError, ValueError):