summaryrefslogtreecommitdiffstats
path: root/ipalib/errors.py
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-08-29 07:05:06 +0000
committerJason Gerard DeRose <jderose@redhat.com>2008-08-29 07:05:06 +0000
commit44ff0b3d23c0473106a6c0da90cc8d80df98ee78 (patch)
tree1a04f70398f71f6b40d5398d4a58a4adbb1165a8 /ipalib/errors.py
parent76b30dff15de9eb50f0d9cb00b6df18ecd91a8f5 (diff)
downloadfreeipa-44ff0b3d23c0473106a6c0da90cc8d80df98ee78.tar.gz
freeipa-44ff0b3d23c0473106a6c0da90cc8d80df98ee78.tar.xz
freeipa-44ff0b3d23c0473106a6c0da90cc8d80df98ee78.zip
224: Reworked IPATypeError class into raise_TypeError function
Diffstat (limited to 'ipalib/errors.py')
-rw-r--r--ipalib/errors.py41
1 files changed, 23 insertions, 18 deletions
diff --git a/ipalib/errors.py b/ipalib/errors.py
index f88fdd5a8..dea7cd730 100644
--- a/ipalib/errors.py
+++ b/ipalib/errors.py
@@ -22,40 +22,45 @@ All custom errors raised by `ipalib` package.
"""
-class IPATypeError(TypeError):
+def raise_TypeError(name, type_, value):
"""
- A TypeError subclass with a helpful message format.
+ Raises a TypeError with a nicely formatted message and helpful attributes.
- IPATypeError has three custom instance attributes:
+ The TypeError raised will have three custom attributes:
- ``name`` - Name of the argument TypeError is being raised for.
+ ``name`` - The name (identifier) of the argument in question.
- ``type`` - Type that the argument should be.
+ ``type`` - The type expected for the arguement.
- ``value`` - Value (of incorrect type) supplied for the argument.
+ ``value`` - The value (of incorrect type) revieved for argument.
- There is no edict that all TypeError should be raised with IPATypeError,
+ There is no edict that all TypeError should be raised with raise_TypeError,
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')
+ >>> raise_TypeError('message', str, u'Hello.')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
- IPATypeError: islate: need a <type 'bool'>; got '4 AM'
+ File "/home/jderose/projects/freeipa2/ipalib/errors.py", line 61, in raise_TypeError
+ raise e
+ TypeError: message: need a <type 'str'>; got u'Hello.'
+
+ :param name: The name (identifier) of the argument in question.
+ :param type_: The type expected for the arguement.
+ :param value: The value (of incorrect type) revieved for argument.
"""
format = '%s: need a %r; got %r'
-
- 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 % (name, type_, value))
+ assert type(name) is str, format % ('name', str, name)
+ assert type(type_) is type, format % ('type_', type, type_)
+ assert type(value) is not type_, 'value: %r is a %r' % (value, type_)
+ e = TypeError(format % (name, type_, value))
+ setattr(e, 'name', name)
+ setattr(e, 'type', type_)
+ setattr(e, 'value', value)
+ raise e
class IPAError(Exception):