mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
adding objects-specific errors
This commit is contained in:
parent
c481170b97
commit
9d72eaf675
|
@ -1,6 +1,7 @@
|
||||||
"""Catalog module."""
|
"""Catalog module."""
|
||||||
|
|
||||||
from .providers import Provider
|
from .providers import Provider
|
||||||
|
from .errors import Error
|
||||||
|
|
||||||
|
|
||||||
class AbstractCatalog(object):
|
class AbstractCatalog(object):
|
||||||
|
@ -20,8 +21,8 @@ class AbstractCatalog(object):
|
||||||
return attribute
|
return attribute
|
||||||
|
|
||||||
if attribute not in self.__used_providers__:
|
if attribute not in self.__used_providers__:
|
||||||
raise AttributeError('Provider \'{}\' is not listed in '
|
raise Error('Provider \'{}\' '.format(item) +
|
||||||
'dependencies'.format(item))
|
'is not listed in dependencies')
|
||||||
return attribute
|
return attribute
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
6
objects/errors.py
Normal file
6
objects/errors.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
"""Errors module."""
|
||||||
|
|
||||||
|
|
||||||
|
class Error(Exception):
|
||||||
|
|
||||||
|
"""Base error."""
|
|
@ -8,6 +8,8 @@ from .utils import is_init_arg_injection
|
||||||
from .utils import is_attribute_injection
|
from .utils import is_attribute_injection
|
||||||
from .utils import is_method_injection
|
from .utils import is_method_injection
|
||||||
|
|
||||||
|
from .errors import Error
|
||||||
|
|
||||||
|
|
||||||
class Provider(object):
|
class Provider(object):
|
||||||
|
|
||||||
|
@ -31,8 +33,8 @@ class Provider(object):
|
||||||
def override(self, provider):
|
def override(self, provider):
|
||||||
"""Override provider with another provider."""
|
"""Override provider with another provider."""
|
||||||
if not is_provider(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)))
|
'got {}'.format(str(provider)))
|
||||||
self.overridden.append(provider)
|
self.overridden.append(provider)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -41,8 +43,8 @@ class Provider(object):
|
||||||
try:
|
try:
|
||||||
return self.overridden[-1]
|
return self.overridden[-1]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
raise AttributeError('Provider {} '.format(str(self)) +
|
raise Error('Provider {} '.format(str(self)) +
|
||||||
'is not overridden')
|
'is not overridden')
|
||||||
|
|
||||||
|
|
||||||
class ProviderDelegate(Provider):
|
class ProviderDelegate(Provider):
|
||||||
|
@ -162,8 +164,8 @@ class Scoped(Singleton):
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
"""Return provided instance."""
|
"""Return provided instance."""
|
||||||
if not self.is_in_scope:
|
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')
|
'while provider is not in scope')
|
||||||
return super(Scoped, self).__call__(*args, **kwargs)
|
return super(Scoped, self).__call__(*args, **kwargs)
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
@ -197,7 +199,7 @@ class ExternalDependency(Provider):
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
"""Return provided instance."""
|
"""Return provided instance."""
|
||||||
if not self.dependency:
|
if not self.dependency:
|
||||||
raise ValueError('Dependency is not satisfied')
|
raise Error('Dependency is not satisfied')
|
||||||
|
|
||||||
result = self.dependency.__call__(*args, **kwargs)
|
result = self.dependency.__call__(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -205,8 +207,8 @@ class ExternalDependency(Provider):
|
||||||
for possible_type in self.instance_of))
|
for possible_type in self.instance_of))
|
||||||
|
|
||||||
if not is_instance:
|
if not is_instance:
|
||||||
raise TypeError('{} is not an '.format(result) +
|
raise Error('{} is not an '.format(result) +
|
||||||
'instance of {}'.format(self.instance_of))
|
'instance of {}'.format(self.instance_of))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,8 @@ from objects.providers import Config
|
||||||
|
|
||||||
from objects.utils import is_provider
|
from objects.utils import is_provider
|
||||||
|
|
||||||
|
from objects.errors import Error
|
||||||
|
|
||||||
|
|
||||||
class ProviderTest(unittest.TestCase):
|
class ProviderTest(unittest.TestCase):
|
||||||
|
|
||||||
|
@ -51,7 +53,7 @@ class ProviderTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_override_with_not_provider(self):
|
def test_override_with_not_provider(self):
|
||||||
"""Test provider overriding with not provider instance."""
|
"""Test provider overriding with not provider instance."""
|
||||||
self.assertRaises(TypeError, Provider().override, object())
|
self.assertRaises(Error, Provider().override, object())
|
||||||
|
|
||||||
def test_last_overriding(self):
|
def test_last_overriding(self):
|
||||||
"""Test getting last overriding provider."""
|
"""Test getting last overriding provider."""
|
||||||
|
@ -69,7 +71,7 @@ class ProviderTest(unittest.TestCase):
|
||||||
"""Test getting last overriding from not overridden provider."""
|
"""Test getting last overriding from not overridden provider."""
|
||||||
try:
|
try:
|
||||||
Provider().last_overriding
|
Provider().last_overriding
|
||||||
except AttributeError:
|
except Error:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
self.fail('Got en error in {}'.format(
|
self.fail('Got en error in {}'.format(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user