[libvirt] [sandbox 11/11] virt-sandbox-image: add error handling for sources import

Cédric Bosdonnat cbosdonnat at suse.com
Wed Sep 23 07:53:41 UTC 2015


Rather than letting the python exception hit the user, catch them and
provide a more meaningful message if no or a bad scheme is provided in
the URI.
---
 libvirt-sandbox/image/template.py | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/libvirt-sandbox/image/template.py b/libvirt-sandbox/image/template.py
index efdd845..58904a2 100644
--- a/libvirt-sandbox/image/template.py
+++ b/libvirt-sandbox/image/template.py
@@ -59,15 +59,21 @@ class Template(object):
             self.params = {}
 
     def get_source_impl(self):
-        p = re.compile("\W")
-        sourcename = "".join([i.capitalize() for i in p.split(self.source)])
-
-        mod = importlib.import_module(
-            "libvirt_sandbox.image.sources." +
-            sourcename + "Source")
-        classname = sourcename + "Source"
-        classimpl = getattr(mod, classname)
-        return classimpl()
+        if self.source == "":
+            raise Exception("Missing scheme in image URI")
+
+        try:
+            p = re.compile("\W")
+            sourcename = "".join([i.capitalize() for i in p.split(self.source)])
+
+            mod = importlib.import_module(
+                "libvirt_sandbox.image.sources." +
+                sourcename + "Source")
+            classname = sourcename + "Source"
+            classimpl = getattr(mod, classname)
+            return classimpl()
+        except Exception:
+            raise Exception("Invalid source: '%s'" % self.source)
 
     def __repr__(self):
         if self.protocol is not None:
-- 
2.1.4




More information about the libvir-list mailing list