mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
adding objects-specific errors
This commit is contained in:
parent
c481170b97
commit
9d72eaf675
|
@ -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
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_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
|
||||
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue
Block a user