From 407ada2367003516c06cb3a4cb6ca9dcc8e2eb75 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Mon, 26 May 2014 16:27:15 +0000 Subject: [PATCH] better error handling on import statement itself --- simport/__init__.py | 9 ++++++++- tests/test_simport.py | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/simport/__init__.py b/simport/__init__.py index afcfa9a..49c37ed 100644 --- a/simport/__init__.py +++ b/simport/__init__.py @@ -35,6 +35,10 @@ class MissingMethodOrFunction(Exception): pass +class ImportFailed(Exception): + pass + + def _get_module(target): """Import a named class, module, method or function. @@ -74,7 +78,10 @@ def _get_module(target): if not class_or_function: raise MissingMethodOrFunction("No Method or Function specified in '%s'" % target) - __import__(module) + try: + __import__(module) + except ImportError as e: + raise ImportFailed("Failed to import '%s'. Error: %s" % (module, e)) klass, sep, function = class_or_function.rpartition('.') return module, klass, function diff --git a/tests/test_simport.py b/tests/test_simport.py index a7ce329..5f45f71 100644 --- a/tests/test_simport.py +++ b/tests/test_simport.py @@ -39,7 +39,7 @@ class TestSimport(unittest.TestCase): simport._get_module("tests|" "localmodule:Foo.method_a")) - self.assertRaises(ImportError, simport._get_module, + self.assertRaises(simport.ImportFailed, simport._get_module, "tests|that_module:function_a") def test_bad_load(self):