adding objects-specific errors

This commit is contained in:
Roman Mogilatov 2015-03-13 17:31:07 +02:00
parent c481170b97
commit 9d72eaf675
4 changed files with 24 additions and 13 deletions

View File

@ -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

6
objects/errors.py Normal file
View File

@ -0,0 +1,6 @@
"""Errors module."""
class Error(Exception):
"""Base error."""

View File

@ -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,7 +33,7 @@ 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, '
raise Error('Expected provider as an overriding instance, '
'got {}'.format(str(provider)))
self.overridden.append(provider)
@ -41,7 +43,7 @@ class Provider(object):
try:
return self.overridden[-1]
except IndexError:
raise AttributeError('Provider {} '.format(str(self)) +
raise Error('Provider {} '.format(str(self)) +
'is not overridden')
@ -162,7 +164,7 @@ 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) +
raise Error('Trying to provide {} '.format(self.provides) +
'while provider is not in scope')
return super(Scoped, self).__call__(*args, **kwargs)
@ -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,7 +207,7 @@ class ExternalDependency(Provider):
for possible_type in self.instance_of))
if not is_instance:
raise TypeError('{} is not an '.format(result) +
raise Error('{} is not an '.format(result) +
'instance of {}'.format(self.instance_of))
return result

View File

@ -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(