Exceptions¶
This reference documents the custom exception classes provided by pico-fastapi. These exceptions signal failures during application startup, and they allow you to handle these conditions explicitly in your code.
Overview¶
PicoFastAPIErroris the common base class for all custom errors in pico-fastapi.NoControllersFoundErroris raised when the controller discovery process completes without finding any controllers.
Catching the base class lets you handle all pico-fastapi-specific errors in one place.
Typical import:
PicoFastAPIError¶
Base class for all pico-fastapi exceptions.
- Use this to catch any pico-fastapi-specific error without matching a concrete subclass.
- When writing library or application code on top of pico-fastapi, prefer catching
PicoFastAPIErrorinstead of broad exceptions likeException, to avoid masking unrelated errors.
Example:
try:
bootstrap_app()
except PicoFastAPIError as exc:
# Handle configuration or discovery errors uniformly
logger.error(f"pico_fastapi startup failed: {exc}")
raise
NoControllersFoundError¶
Raised when the system cannot find any controllers during discovery.
Exact message:
Constructor signature: - NoControllersFoundError() (takes no arguments)
When raised: - By register_controllers() after scanning the container for controllers and finding none.
Example usage during discovery:
from pico_fastapi.exceptions import NoControllersFoundError
try:
register_controllers(app, container)
except NoControllersFoundError:
logger.warning("No controllers found; the API will expose no endpoints.")
Removed Exceptions¶
InvalidConfigurerError (removed in v0.2.2)¶
InvalidConfigurerError was removed in v0.2.2. Invalid configurers are now logged as a warning and silently discarded. See the Migration Guide for details.
Recommendations¶
- Catch
PicoFastAPIErrorat application boundaries (e.g., startup) to handle all pico-fastapi-specific failures in a single place. - Do not rely on the exact exception message in code paths; match on the exception class to make your code resilient to message changes.
Auto-generated API¶
pico_fastapi.exceptions ¶
Custom exceptions for pico-fastapi.
All pico-fastapi exceptions inherit from :class:PicoFastAPIError, allowing callers to catch the base class at application boundaries.
PicoFastAPIError ¶
Bases: Exception
Base exception for all pico-fastapi errors.
Catch this at startup boundaries to handle any pico-fastapi failure without matching individual subclasses.
Example
.. code-block:: python
try:
app = container.get(FastAPI)
except PicoFastAPIError as exc:
logger.error("pico-fastapi startup failed: %s", exc)
raise
Source code in src/pico_fastapi/exceptions.py
NoControllersFoundError ¶
Bases: PicoFastAPIError
Raised when no @controller-decorated classes are found at startup.
This error is raised by register_controllers() when the pico-ioc container does not contain any classes marked with the @controller decorator.
Causes
- Controller modules were not included in
init(modules=[...]). - Controller classes are missing the
@controllerdecorator. - Import errors prevented controller modules from loading.
Example
.. code-block:: python
try:
register_controllers(app, container)
except NoControllersFoundError:
logger.warning("No controllers found; API has no endpoints.")