Skip to content

Exceptions

InjectableNotResolved

Bases: RuntimeError, LagomException

An instance of injectable was consumed in some user code. This should not occur as lagom should have replaced the injectable with an object. This likely means the function wasn't bound to an injection container.

Source code in lagom/exceptions.py
16
17
18
19
20
21
22
23
24
class InjectableNotResolved(RuntimeError, LagomException):
    """
    An instance of injectable was consumed in some user code.
    This should not occur as lagom should have replaced the injectable
    with an object. This likely means the function wasn't bound to
    an injection container.
    """

    pass

InvalidDependencyDefinition

Bases: ValueError, LagomException

The provided construction logic is not valid

Source code in lagom/exceptions.py
27
28
29
30
class InvalidDependencyDefinition(ValueError, LagomException):
    """The provided construction logic is not valid"""

    pass

ClassesCannotBeDecorated

Bases: SyntaxError, LagomException

Decorating classes is not supported by lagom

Source code in lagom/exceptions.py
33
34
35
36
37
38
39
40
41
42
43
class ClassesCannotBeDecorated(SyntaxError, LagomException):
    """Decorating classes is not supported by lagom"""

    dep_type: str

    def __init__(self):
        super().__init__(
            "Decorating classes is not supported by lagom. \n"
            "Alternative is to create a factory method and use that: \n"
            "factory_func = container.partial(ClassName)"
        )

MissingReturnType

Bases: SyntaxError, LagomException

The function provided doesnt type hint a return

Source code in lagom/exceptions.py
46
47
48
49
class MissingReturnType(SyntaxError, LagomException):
    """The function provided doesnt type hint a return"""

    pass

DuplicateDefinition

Bases: ValueError, LagomException

The type has already been defined somewhere else

Source code in lagom/exceptions.py
52
53
54
55
class DuplicateDefinition(ValueError, LagomException):
    """The type has already been defined somewhere else"""

    pass

UnableToInvokeBoundFunction

Bases: TypeError, LagomException

A function bound to the container could not be invoked

Source code in lagom/exceptions.py
76
77
78
79
80
81
82
83
84
85
86
class UnableToInvokeBoundFunction(TypeError, LagomException):
    """A function bound to the container could not be invoked"""

    unresolvable_deps: typing.List[Type]

    def __init__(self, msg, unresolvable_deps):
        self.unresolvable_deps = unresolvable_deps
        unresolvable_string_list = ",".join(d.__name__ for d in unresolvable_deps)
        super().__init__(
            f"{msg}. The container could not construct the following types: {unresolvable_string_list}"
        )

UnresolvableType

Bases: ValueError, LagomException

The type cannot be constructed

Source code in lagom/exceptions.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class UnresolvableType(ValueError, LagomException):
    """The type cannot be constructed"""

    dep_type: str

    def __init__(self, dep_type: Type):
        """

        :param dep_type: The type that could not be constructed
        """
        self.dep_type = _dep_type_as_string(dep_type)
        if inspect.isabstract(dep_type):
            super().__init__(
                f"Unable to construct Abstract type {self.dep_type}."
                "Try defining an alias or a concrete class to construct"
            )
        else:
            super().__init__(
                f"Unable to construct dependency of type {self.dep_type} "
                "The constructor probably has some unresolvable dependencies"
            )

    def __str__(self):
        return f"{super().__str__()}: {str.join(' => ', self.get_unresolvable_deps_sequence())}"

    def get_unresolvable_deps_sequence(self) -> typing.List[str]:
        """Returns the dependency stack with the last element being the dependency source of the exception"""
        error: typing.Optional[BaseException] = self
        unresolvable_deps: typing.List[str] = []

        for _loop_guard in range(100):
            if not (error and isinstance(error, UnresolvableType)):
                return unresolvable_deps
            unresolvable_deps.append(error.dep_type)
            error = error.__cause__
        # This should never happen
        unresolvable_deps.append("...")
        return unresolvable_deps

TypeResolutionBlocked

Bases: UnresolvableType

The type was explicitly blocked by configuration

Source code in lagom/exceptions.py
129
130
131
132
133
134
135
136
class TypeResolutionBlocked(UnresolvableType):
    """The type was explicitly blocked by configuration"""

    dep_type: str

    def __init__(self, dep_type: typing.Optional[typing.Type], msg: str):
        self.dep_type = _dep_type_as_string(dep_type) if dep_type else ""
        super(ValueError, self).__init__(msg)

RecursiveDefinitionError

Bases: SyntaxError, LagomException

Whilst trying to resolve the type python exceeded the recursion depth

Source code in lagom/exceptions.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class RecursiveDefinitionError(SyntaxError, LagomException):
    """Whilst trying to resolve the type python exceeded the recursion depth"""

    dep_type: Type

    def __init__(self, dep_type: Type):
        """
        :param dep_type: The type that could not be constructed
        """
        self.dep_type = dep_type
        super().__init__(
            f"When trying to build dependency of type '{_dep_type_as_string(dep_type)}' python hit a recursion limit. "
            "This could indicate a circular definition somewhere."
        )

DependencyNotDefined

Bases: ValueError, LagomException

The type must be explicitly defined in the container

Source code in lagom/exceptions.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
class DependencyNotDefined(ValueError, LagomException):
    """The type must be explicitly defined in the container"""

    dep_type: Type

    def __init__(self, dep_type: Type):
        """
        :param dep_type: The type that was not defined
        """
        self.dep_type = dep_type
        super().__init__(
            f"{_dep_type_as_string(dep_type)} has not been defined. "
            f"In an explict container all dependencies must be defined"
        )

MissingEnvVariable

Bases: LagomException

Whilst trying to load settings from environment variables one or more required variables had not been set. More documentation for this can be found in the lagom.environment module.

Source code in lagom/exceptions.py
171
172
173
174
175
176
177
178
179
180
class MissingEnvVariable(LagomException):
    """
    Whilst trying to load settings from environment variables one or more required variables had not been set.
    More documentation for this can be found in the lagom.environment module.
    """

    def __init__(self, variable_names: typing.List[str]):
        super().__init__(
            f"Expected environment variables not found: {', '.join(variable_names)}"
        )

InvalidEnvironmentVariables

Bases: LagomException

Whilst trying to load settings from environment variables one or more variables failed the validation rules. Internally the pydantic library is used to coerce and validate the environment variable from a string into a python data type. More documentation for this can be found in the lagom.environment module.

Source code in lagom/exceptions.py
183
184
185
186
187
188
189
190
191
192
193
194
class InvalidEnvironmentVariables(LagomException):
    """
    Whilst trying to load settings from environment variables one or more variables failed the validation rules.
    Internally the pydantic library is used to coerce and validate the environment variable from a string into
    a python data type.
    More documentation for this can be found in the lagom.environment module.
    """

    def __init__(self, variable_names: typing.List[str], details: str):
        super().__init__(
            f"Unable to load environment variables: {', '.join(variable_names)} \n {details}"
        )