diff --git a/objects/catalog.py b/objects/catalog.py index 67b42064..9c88b15a 100644 --- a/objects/catalog.py +++ b/objects/catalog.py @@ -1,6 +1,7 @@ """Catalog module.""" from .providers import Provider +from .errors import Error class AbstractCatalog(object): @@ -20,8 +21,8 @@ class AbstractCatalog(object): return attribute if attribute not in self.__used_providers__: - raise AttributeError('Provider \'{}\' is not listed in ' - 'dependencies'.format(item)) + raise Error('Provider \'{}\' '.format(item) + + 'is not listed in dependencies') return attribute @classmethod diff --git a/objects/errors.py b/objects/errors.py new file mode 100644 index 00000000..b5cc1414 --- /dev/null +++ b/objects/errors.py @@ -0,0 +1,6 @@ +"""Errors module.""" + + +class Error(Exception): + + """Base error.""" diff --git a/objects/providers.py b/objects/providers.py index 97e5acae..17cd1e4a 100644 --- a/objects/providers.py +++ b/objects/providers.py @@ -8,6 +8,8 @@ from .utils import is_init_arg_injection from .utils import is_attribute_injection from .utils import is_method_injection +from .errors import Error + class Provider(object): @@ -31,8 +33,8 @@ class Provider(object): def override(self, provider): """Override provider with another provider.""" if not is_provider(provider): - raise TypeError('Expected provider as an overriding instance, ' - 'got {}'.format(str(provider))) + raise Error('Expected provider as an overriding instance, ' + 'got {}'.format(str(provider))) self.overridden.append(provider) @property @@ -41,8 +43,8 @@ class Provider(object): try: return self.overridden[-1] except IndexError: - raise AttributeError('Provider {} '.format(str(self)) + - 'is not overridden') + raise Error('Provider {} '.format(str(self)) + + 'is not overridden') class ProviderDelegate(Provider): @@ -162,8 +164,8 @@ class Scoped(Singleton): def __call__(self, *args, **kwargs): """Return provided instance.""" if not self.is_in_scope: - raise RuntimeError('Trying to provide {} '.format(self.provides) + - 'while provider is not in scope') + raise Error('Trying to provide {} '.format(self.provides) + + 'while provider is not in scope') return super(Scoped, self).__call__(*args, **kwargs) def __enter__(self): @@ -197,7 +199,7 @@ class ExternalDependency(Provider): def __call__(self, *args, **kwargs): """Return provided instance.""" if not self.dependency: - raise ValueError('Dependency is not satisfied') + raise Error('Dependency is not satisfied') result = self.dependency.__call__(*args, **kwargs) @@ -205,8 +207,8 @@ class ExternalDependency(Provider): for possible_type in self.instance_of)) if not is_instance: - raise TypeError('{} is not an '.format(result) + - 'instance of {}'.format(self.instance_of)) + raise Error('{} is not an '.format(result) + + 'instance of {}'.format(self.instance_of)) return result diff --git a/tests/test_providers.py b/tests/test_providers.py index ae801cf0..36afb49c 100644 --- a/tests/test_providers.py +++ b/tests/test_providers.py @@ -17,6 +17,8 @@ from objects.providers import Config from objects.utils import is_provider +from objects.errors import Error + class ProviderTest(unittest.TestCase): @@ -51,7 +53,7 @@ class ProviderTest(unittest.TestCase): def test_override_with_not_provider(self): """Test provider overriding with not provider instance.""" - self.assertRaises(TypeError, Provider().override, object()) + self.assertRaises(Error, Provider().override, object()) def test_last_overriding(self): """Test getting last overriding provider.""" @@ -69,7 +71,7 @@ class ProviderTest(unittest.TestCase): """Test getting last overriding from not overridden provider.""" try: Provider().last_overriding - except AttributeError: + except Error: pass else: self.fail('Got en error in {}'.format(