summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ipalib/errors.py29
-rw-r--r--ipalib/tests/test_errors.py29
2 files changed, 42 insertions, 16 deletions
diff --git a/ipalib/errors.py b/ipalib/errors.py
index 52225373..f88fdd5a 100644
--- a/ipalib/errors.py
+++ b/ipalib/errors.py
@@ -24,25 +24,38 @@ All custom errors raised by `ipalib` package.
class IPATypeError(TypeError):
"""
- A TypeError subclass with with a standard message.
+ A TypeError subclass with a helpful message format.
- Also has two custom attributes:
+ IPATypeError has three custom instance attributes:
- ``type`` - The type being tested against.
- ``value`` - The offending value that caused the exception.
+ ``name`` - Name of the argument TypeError is being raised for.
+
+ ``type`` - Type that the argument should be.
+
+ ``value`` - Value (of incorrect type) supplied for the argument.
There is no edict that all TypeError should be raised with IPATypeError,
but when it fits, use it... it makes the unit tests faster to write and
the debugging easier to read.
+
+ Here is an example:
+
+ >>> raise IPATypeError('islate', bool, '4 AM')
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+ IPATypeError: islate: need a <type 'bool'>; got '4 AM'
"""
- format = 'need a %r; got %r'
+ format = '%s: need a %r; got %r'
- def __init__(self, type_, value):
- assert type(value) is not type_, '%r is a %r' % (value, type_)
+ def __init__(self, name, type_, value):
+ assert type(name) is str, self.format % ('name', str, name)
+ assert type(type_) is type, self.format % ('type_', type, type_)
+ assert type(value) is not type_, 'value: %r is a %r' % (value, type_)
+ self.name = name
self.type = type_
self.value = value
- TypeError.__init__(self, self.format % (self.type, self.value))
+ TypeError.__init__(self, self.format % (name, type_, value))
class IPAError(Exception):
diff --git a/ipalib/tests/test_errors.py b/ipalib/tests/test_errors.py
index 48b1b8fe..2d5fc613 100644
--- a/ipalib/tests/test_errors.py
+++ b/ipalib/tests/test_errors.py
@@ -38,14 +38,27 @@ class test_IPATypeError(ClassChecker):
"""
Tests the `errors.IPATypeError.__init__` method.
"""
+ format = '%s: need a %r; got %r'
+ name = 'message'
type_ = unicode
- okay = 'hello'
- e = self.cls(type_, okay)
+ value = 'hello world'
+ e = self.cls(name, type_, value)
+ assert e.name is name
assert e.type is type_
- assert e.value is okay
- assert str(e) == 'need a %r; got %r' % (type_, okay)
+ assert e.value is value
+ assert str(e) == format % (name, type_, value)
- # Check that AssertionError is raised when type(value) is type_:
- fail = u'hello'
- e = raises(AssertionError, self.cls, type_, fail)
- assert str(e) == '%r is a %r' % (fail, type_)
+ # name not an str:
+ fail = 42
+ e = raises(AssertionError, self.cls, fail, type_, value)
+ assert str(e) == format % ('name', str, fail)
+
+ # type_ not a type:
+ fail = unicode()
+ e = raises(AssertionError, self.cls, name, fail, value)
+ assert str(e) == format % ('type_', type, fail)
+
+ # type(value) is type_:
+ fail = u'how are you?'
+ e = raises(AssertionError, self.cls, name, type_, fail)
+ assert str(e) == 'value: %r is a %r' % (fail, type_)