Errors & troubleshooting

What you’ll learn

  • What the most common error categories look like at runtime.

Missing dependency error

Run locally

uv run python examples/ex_16_errors_and_troubleshooting/01_missing_dependency_error.py
"""Focused example: ``DIWireDependencyNotRegisteredError``."""

from __future__ import annotations

from diwire import Container, DependencyRegistrationPolicy, MissingPolicy
from diwire.exceptions import DIWireDependencyNotRegisteredError


class MissingDependency:
    pass


def main() -> None:
    container = Container(
        missing_policy=MissingPolicy.ERROR,
        dependency_registration_policy=DependencyRegistrationPolicy.IGNORE,
    )

    try:
        container.resolve(MissingDependency)
    except DIWireDependencyNotRegisteredError as error:
        error_name = type(error).__name__

    print(f"missing={error_name}")  # => missing=DIWireDependencyNotRegisteredError


if __name__ == "__main__":
    main()

Scope mismatch error

Run locally

uv run python examples/ex_16_errors_and_troubleshooting/02_scope_mismatch_error.py
"""Focused example: ``DIWireScopeMismatchError``."""

from __future__ import annotations

from diwire import Container, Scope
from diwire.exceptions import DIWireScopeMismatchError


class RequestDependency:
    pass


def main() -> None:
    container = Container()
    container.add(
        RequestDependency,
        provides=RequestDependency,
        scope=Scope.REQUEST,
    )

    try:
        container.resolve(RequestDependency)
    except DIWireScopeMismatchError as error:
        error_name = type(error).__name__

    print(f"scope={error_name}")  # => scope=DIWireScopeMismatchError


if __name__ == "__main__":
    main()

Async-in-sync error

Run locally

uv run python examples/ex_16_errors_and_troubleshooting/03_async_in_sync_error.py
"""Focused example: ``DIWireAsyncDependencyInSyncContextError``."""

from __future__ import annotations

from diwire import Container
from diwire.exceptions import DIWireAsyncDependencyInSyncContextError


class AsyncDependency:
    pass


async def provide_async_dependency() -> AsyncDependency:
    return AsyncDependency()


def main() -> None:
    container = Container()
    container.add_factory(provide_async_dependency, provides=AsyncDependency)

    try:
        container.resolve(AsyncDependency)
    except DIWireAsyncDependencyInSyncContextError as error:
        error_name = type(error).__name__

    print(f"async_in_sync={error_name}")  # => async_in_sync=DIWireAsyncDependencyInSyncContextError


if __name__ == "__main__":
    main()

Inference error

Run locally

uv run python examples/ex_16_errors_and_troubleshooting/04_inference_error.py
"""Focused example: ``DIWireProviderDependencyInferenceError``."""

from __future__ import annotations

from diwire import Container
from diwire.exceptions import DIWireProviderDependencyInferenceError


class Service:
    pass


def build_service(raw_value) -> Service:  # type: ignore[no-untyped-def]
    _ = raw_value
    return Service()


def main() -> None:
    container = Container()

    try:
        container.add_factory(build_service, provides=Service)
    except DIWireProviderDependencyInferenceError as error:
        error_name = type(error).__name__

    print(f"inference={error_name}")  # => inference=DIWireProviderDependencyInferenceError


if __name__ == "__main__":
    main()

Generic error

Run locally

uv run python examples/ex_16_errors_and_troubleshooting/05_generic_error.py
"""Focused example: ``DIWireInvalidGenericTypeArgumentError``."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Generic, TypeVar, cast

from diwire import Container
from diwire.exceptions import DIWireInvalidGenericTypeArgumentError


class Model:
    pass


M = TypeVar("M", bound=Model)


class ModelBox(Generic[M]):
    pass


@dataclass(slots=True)
class DefaultModelBox(ModelBox[M]):
    type_arg: type[M]


def main() -> None:
    container = Container()
    container.add(DefaultModelBox, provides=ModelBox)

    invalid_key = cast("Any", ModelBox)[str]
    try:
        container.resolve(invalid_key)
    except DIWireInvalidGenericTypeArgumentError as error:
        error_name = type(error).__name__

    print(f"generic={error_name}")  # => generic=DIWireInvalidGenericTypeArgumentError


if __name__ == "__main__":
    main()

Invalid registration error

Run locally

uv run python examples/ex_16_errors_and_troubleshooting/06_invalid_registration_error.py
"""Focused example: ``DIWireInvalidRegistrationError``."""

from __future__ import annotations

from typing import Any, cast

from diwire import Container
from diwire.exceptions import DIWireInvalidRegistrationError


def main() -> None:
    container = Container()

    try:
        container.add_instance(object(), provides=cast("Any", None))
    except DIWireInvalidRegistrationError as error:
        error_name = type(error).__name__

    print(f"invalid_reg={error_name}")  # => invalid_reg=DIWireInvalidRegistrationError


if __name__ == "__main__":
    main()