Fixes https://fedorahosted.org/freeipa/ticket/5473
Patch is for master only, I will rebase it for 4-2 if we decide that it can land there as well.
-- Martin^3 Babinsky
From 9072fad67fd0bc3b9ef08d59a0c59c949a78e87a Mon Sep 17 00:00:00 2001 From: Martin Babinsky <mbabi...@redhat.com> Date: Fri, 20 Nov 2015 13:47:34 +0100 Subject: [PATCH] raise more descriptive Backend connection-related exceptions https://fedorahosted.org/freeipa/ticket/5473 --- ipalib/backend.py | 20 ++++++++++++++------ ipatests/test_ipalib/test_backend.py | 16 +++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/ipalib/backend.py b/ipalib/backend.py index b8fa29626ea3e9eaa0537e7c09d80d89bd8aa83e..342e17ec5010b546cfb97063cbcddf570c85d70b 100644 --- a/ipalib/backend.py +++ b/ipalib/backend.py @@ -57,8 +57,10 @@ class Connectible(Backend): """ if hasattr(context, self.id): raise Exception( - "connect: 'context.%s' already exists in thread %r" % ( - self.id, threading.currentThread().getName() + "{0} is already connected ({1} in {2})".format( + self.name, + self.id, + threading.currentThread().getName() ) ) conn = self.create_connection(*args, **kw) @@ -72,8 +74,10 @@ class Connectible(Backend): def disconnect(self): if not hasattr(context, self.id): raise Exception( - "disconnect: 'context.%s' does not exist in thread %r" % ( - self.id, threading.currentThread().getName() + "{0} is not connected ({1} in {2})".format( + self.name, + self.id, + threading.currentThread().getName() ) ) self.destroy_connection() @@ -94,8 +98,12 @@ class Connectible(Backend): Return thread-local connection. """ if not hasattr(context, self.id): - raise AttributeError('no context.%s in thread %r' % ( - self.id, threading.currentThread().getName()) + raise AttributeError( + "{0} is not connected ({1} in {2})".format( + self.name, + self.id, + threading.currentThread().getName() + ) ) return getattr(context, self.id).conn conn = property(__get_conn) diff --git a/ipatests/test_ipalib/test_backend.py b/ipatests/test_ipalib/test_backend.py index 086831037a81b917324ce419dc43d41a016b4473..f758d4ac7c68d562a8ce0eb2165e9457d362fdfd 100644 --- a/ipatests/test_ipalib/test_backend.py +++ b/ipatests/test_ipalib/test_backend.py @@ -93,9 +93,10 @@ class test_Connectible(ClassChecker): assert conn.disconnect == o.disconnect # 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) - 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: del context.example @@ -122,9 +123,10 @@ class test_Connectible(ClassChecker): destroy_connection = Disconnect() 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) - assert str(e) == m % ('example', threading.currentThread().getName()) + assert str(e) == m.format( + 'example', o.id, threading.currentThread().getName()) context.example = 'The connection.' assert o.disconnect() is None @@ -162,14 +164,14 @@ class test_Connectible(ClassChecker): Test the `ipalib.backend.Connectible.conn` property. """ api = 'the api instance' - msg = 'no context.%s in thread %r' + msg = '{0} is not connected ({1} in {2})' class example(self.cls): pass for klass in (self.cls, example): o = klass(api, shared_instance=True) e = raises(AttributeError, getattr, o, 'conn') - assert str(e) == msg % ( - klass.__name__, threading.currentThread().getName() + assert str(e) == msg.format( + klass.__name__, o.id, threading.currentThread().getName() ) conn = Connection('The connection.', Disconnect()) setattr(context, klass.__name__, conn) -- 2.4.3
-- Manage your subscription for the Freeipa-devel mailing list: https://www.redhat.com/mailman/listinfo/freeipa-devel Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code