Common: Use a more precise, custom UnregisteredTypeError in DIContainer
This commit is contained in:
parent
7382407be0
commit
c16705241a
|
@ -4,6 +4,10 @@ from typing import Any, MutableMapping, Sequence, Type, TypeVar
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
|
class UnregisteredTypeError(ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class DIContainer:
|
class DIContainer:
|
||||||
"""
|
"""
|
||||||
A dependency injection (DI) container that uses type annotations to resolve and inject
|
A dependency injection (DI) container that uses type annotations to resolve and inject
|
||||||
|
@ -45,7 +49,7 @@ class DIContainer:
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return self._resolve_type(type_)
|
return self._resolve_type(type_)
|
||||||
except ValueError:
|
except UnregisteredTypeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
args = self.resolve_dependencies(type_)
|
args = self.resolve_dependencies(type_)
|
||||||
|
@ -74,7 +78,7 @@ class DIContainer:
|
||||||
elif type_ in self._instance_registry:
|
elif type_ in self._instance_registry:
|
||||||
return self._retrieve_registered_instance(type_)
|
return self._retrieve_registered_instance(type_)
|
||||||
|
|
||||||
raise ValueError(f'Failed to resolve unknown type "{type_.__name__}"')
|
raise UnregisteredTypeError(f'Failed to resolve unregistered type "{type_.__name__}"')
|
||||||
|
|
||||||
def _construct_new_instance(self, arg_type: Type[T]) -> T:
|
def _construct_new_instance(self, arg_type: Type[T]) -> T:
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue