mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
224: Reworked IPATypeError class into raise_TypeError function
This commit is contained in:
@@ -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
|
but when it fits, use it... it makes the unit tests faster to write and
|
||||||
the debugging easier to read.
|
the debugging easier to read.
|
||||||
|
|
||||||
Here is an example:
|
Here is an example:
|
||||||
|
|
||||||
>>> raise IPATypeError('islate', bool, '4 AM')
|
>>> raise_TypeError('message', str, u'Hello.')
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
File "<stdin>", line 1, in <module>
|
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'
|
format = '%s: need a %r; got %r'
|
||||||
|
assert type(name) is str, format % ('name', str, name)
|
||||||
def __init__(self, name, type_, value):
|
assert type(type_) is type, format % ('type_', type, type_)
|
||||||
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_)
|
assert type(value) is not type_, 'value: %r is a %r' % (value, type_)
|
||||||
self.name = name
|
e = TypeError(format % (name, type_, value))
|
||||||
self.type = type_
|
setattr(e, 'name', name)
|
||||||
self.value = value
|
setattr(e, 'type', type_)
|
||||||
TypeError.__init__(self, self.format % (name, type_, value))
|
setattr(e, 'value', value)
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
class IPAError(Exception):
|
class IPAError(Exception):
|
||||||
|
|||||||
@@ -25,24 +25,16 @@ from tstutil import raises, ClassChecker
|
|||||||
from ipalib import errors
|
from ipalib import errors
|
||||||
|
|
||||||
|
|
||||||
class test_IPATypeError(ClassChecker):
|
def test_raise_TypeError():
|
||||||
"""
|
"""
|
||||||
Tests the `errors.IPATypeError` exception.
|
Tests the `errors.raise_TypeError` function.
|
||||||
"""
|
|
||||||
_cls = errors.IPATypeError
|
|
||||||
|
|
||||||
def test_class(self):
|
|
||||||
assert self.cls.__bases__ == (TypeError,)
|
|
||||||
|
|
||||||
def test_init(self):
|
|
||||||
"""
|
|
||||||
Tests the `errors.IPATypeError.__init__` method.
|
|
||||||
"""
|
"""
|
||||||
|
f = errors.raise_TypeError
|
||||||
format = '%s: need a %r; got %r'
|
format = '%s: need a %r; got %r'
|
||||||
name = 'message'
|
name = 'message'
|
||||||
type_ = unicode
|
type_ = unicode
|
||||||
value = 'hello world'
|
value = 'Hello.'
|
||||||
e = self.cls(name, type_, value)
|
e = raises(TypeError, f, name, type_, value)
|
||||||
assert e.name is name
|
assert e.name is name
|
||||||
assert e.type is type_
|
assert e.type is type_
|
||||||
assert e.value is value
|
assert e.value is value
|
||||||
@@ -50,15 +42,15 @@ class test_IPATypeError(ClassChecker):
|
|||||||
|
|
||||||
# name not an str:
|
# name not an str:
|
||||||
fail = 42
|
fail = 42
|
||||||
e = raises(AssertionError, self.cls, fail, type_, value)
|
e = raises(AssertionError, f, fail, type_, value)
|
||||||
assert str(e) == format % ('name', str, fail)
|
assert str(e) == format % ('name', str, fail)
|
||||||
|
|
||||||
# type_ not a type:
|
# type_ not a type:
|
||||||
fail = unicode()
|
fail = unicode()
|
||||||
e = raises(AssertionError, self.cls, name, fail, value)
|
e = raises(AssertionError, f, name, fail, value)
|
||||||
assert str(e) == format % ('type_', type, fail)
|
assert str(e) == format % ('type_', type, fail)
|
||||||
|
|
||||||
# type(value) is type_:
|
# type(value) is type_:
|
||||||
fail = u'how are you?'
|
fail = u'How are you?'
|
||||||
e = raises(AssertionError, self.cls, name, type_, fail)
|
e = raises(AssertionError, f, name, type_, fail)
|
||||||
assert str(e) == 'value: %r is a %r' % (fail, type_)
|
assert str(e) == 'value: %r is a %r' % (fail, type_)
|
||||||
|
|||||||
Reference in New Issue
Block a user