mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
raise more descriptive Backend connection-related exceptions
https://fedorahosted.org/freeipa/ticket/5473 Reviewed-By: Martin Basti <mbasti@redhat.com>
This commit is contained in:
parent
aa648bcedc
commit
cd5fa38945
@ -57,8 +57,10 @@ class Connectible(Backend):
|
|||||||
"""
|
"""
|
||||||
if hasattr(context, self.id):
|
if hasattr(context, self.id):
|
||||||
raise Exception(
|
raise Exception(
|
||||||
"connect: 'context.%s' already exists in thread %r" % (
|
"{0} is already connected ({1} in {2})".format(
|
||||||
self.id, threading.currentThread().getName()
|
self.name,
|
||||||
|
self.id,
|
||||||
|
threading.currentThread().getName()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
conn = self.create_connection(*args, **kw)
|
conn = self.create_connection(*args, **kw)
|
||||||
@ -72,8 +74,10 @@ class Connectible(Backend):
|
|||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
if not hasattr(context, self.id):
|
if not hasattr(context, self.id):
|
||||||
raise Exception(
|
raise Exception(
|
||||||
"disconnect: 'context.%s' does not exist in thread %r" % (
|
"{0} is not connected ({1} in {2})".format(
|
||||||
self.id, threading.currentThread().getName()
|
self.name,
|
||||||
|
self.id,
|
||||||
|
threading.currentThread().getName()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.destroy_connection()
|
self.destroy_connection()
|
||||||
@ -94,8 +98,12 @@ class Connectible(Backend):
|
|||||||
Return thread-local connection.
|
Return thread-local connection.
|
||||||
"""
|
"""
|
||||||
if not hasattr(context, self.id):
|
if not hasattr(context, self.id):
|
||||||
raise AttributeError('no context.%s in thread %r' % (
|
raise AttributeError(
|
||||||
self.id, threading.currentThread().getName())
|
"{0} is not connected ({1} in {2})".format(
|
||||||
|
self.name,
|
||||||
|
self.id,
|
||||||
|
threading.currentThread().getName()
|
||||||
|
)
|
||||||
)
|
)
|
||||||
return getattr(context, self.id).conn
|
return getattr(context, self.id).conn
|
||||||
conn = property(__get_conn)
|
conn = property(__get_conn)
|
||||||
|
@ -93,9 +93,10 @@ class test_Connectible(ClassChecker):
|
|||||||
assert conn.disconnect == o.disconnect
|
assert conn.disconnect == o.disconnect
|
||||||
|
|
||||||
# Test that Exception is raised if already connected:
|
# Test that Exception is raised if already connected:
|
||||||
m = "connect: 'context.%s' already exists in thread %r"
|
m = "{0} is already connected ({1} in {2})"
|
||||||
e = raises(Exception, o.connect, *args, **kw)
|
e = raises(Exception, o.connect, *args, **kw)
|
||||||
assert str(e) == m % ('example', threading.currentThread().getName())
|
assert str(e) == m.format(
|
||||||
|
'example', o.id, threading.currentThread().getName())
|
||||||
|
|
||||||
# Double check that it works after deleting context.example:
|
# Double check that it works after deleting context.example:
|
||||||
del context.example
|
del context.example
|
||||||
@ -122,9 +123,10 @@ class test_Connectible(ClassChecker):
|
|||||||
destroy_connection = Disconnect()
|
destroy_connection = Disconnect()
|
||||||
o = example(api, shared_instance=True)
|
o = example(api, shared_instance=True)
|
||||||
|
|
||||||
m = "disconnect: 'context.%s' does not exist in thread %r"
|
m = "{0} is not connected ({1} in {2})"
|
||||||
e = raises(Exception, o.disconnect)
|
e = raises(Exception, o.disconnect)
|
||||||
assert str(e) == m % ('example', threading.currentThread().getName())
|
assert str(e) == m.format(
|
||||||
|
'example', o.id, threading.currentThread().getName())
|
||||||
|
|
||||||
context.example = 'The connection.'
|
context.example = 'The connection.'
|
||||||
assert o.disconnect() is None
|
assert o.disconnect() is None
|
||||||
@ -162,14 +164,14 @@ class test_Connectible(ClassChecker):
|
|||||||
Test the `ipalib.backend.Connectible.conn` property.
|
Test the `ipalib.backend.Connectible.conn` property.
|
||||||
"""
|
"""
|
||||||
api = 'the api instance'
|
api = 'the api instance'
|
||||||
msg = 'no context.%s in thread %r'
|
msg = '{0} is not connected ({1} in {2})'
|
||||||
class example(self.cls):
|
class example(self.cls):
|
||||||
pass
|
pass
|
||||||
for klass in (self.cls, example):
|
for klass in (self.cls, example):
|
||||||
o = klass(api, shared_instance=True)
|
o = klass(api, shared_instance=True)
|
||||||
e = raises(AttributeError, getattr, o, 'conn')
|
e = raises(AttributeError, getattr, o, 'conn')
|
||||||
assert str(e) == msg % (
|
assert str(e) == msg.format(
|
||||||
klass.__name__, threading.currentThread().getName()
|
klass.__name__, o.id, threading.currentThread().getName()
|
||||||
)
|
)
|
||||||
conn = Connection('The connection.', Disconnect())
|
conn = Connection('The connection.', Disconnect())
|
||||||
setattr(context, klass.__name__, conn)
|
setattr(context, klass.__name__, conn)
|
||||||
|
Loading…
Reference in New Issue
Block a user