Skip to content

main

Tool Inventory API.

This module initializes the FastAPI application and sets up the routes, exception handlers, and static file serving for the tool inventory application.

lifespan async

lifespan(_app: FastAPI) -> AsyncGenerator[None]

Manage the lifespan of the application.

Parameters:

Name Type Description Default
_app FastAPI

The FastAPI application.

required

Yields:

Type Description
AsyncGenerator[None]

None

Source code in src/tool_inventory/main.py
@asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncGenerator[None]:
    """Manage the lifespan of the application.

    Args:
        _app: The FastAPI application.

    Yields:
        None
    """
    logger.remove()
    logger.add(sys.stderr, level="DEBUG")
    yield

object_exists_error_handler async

object_exists_error_handler(_request: Request, exc: ObjectExistsError) -> JSONResponse

Handle ObjectExistsError exceptions.

Parameters:

Name Type Description Default
_request Request

The request object.

required
exc ObjectExistsError

The ObjectExistsError exception.

required

Returns:

Type Description
JSONResponse

A JSON response with the error details.

Source code in src/tool_inventory/main.py
@app.exception_handler(ObjectExistsError)
async def object_exists_error_handler(
    _request: Request,
    exc: ObjectExistsError,
) -> JSONResponse:
    """Handle ObjectExistsError exceptions.

    Args:
        _request: The request object.
        exc: The ObjectExistsError exception.

    Returns:
        A JSON response with the error details.
    """
    return JSONResponse(
        status_code=status.HTTP_409_CONFLICT,
        content={"detail": exc.detail, "object_id": exc.object_id},
    )

object_not_found_error_handler async

object_not_found_error_handler(_request: Request, exc: ObjectNotFoundError) -> JSONResponse

Handle ObjectNotFoundError exceptions.

Parameters:

Name Type Description Default
_request Request

The request object.

required
exc ObjectNotFoundError

The ObjectNotFoundError exception.

required

Returns:

Type Description
JSONResponse

A JSON response with the error details.

Source code in src/tool_inventory/main.py
@app.exception_handler(ObjectNotFoundError)
async def object_not_found_error_handler(
    _request: Request,
    exc: ObjectNotFoundError,
) -> JSONResponse:
    """Handle ObjectNotFoundError exceptions.

    Args:
        _request: The request object.
        exc: The ObjectNotFoundError exception.

    Returns:
        A JSON response with the error details.
    """
    return JSONResponse(
        status_code=status.HTTP_404_NOT_FOUND,
        content={"detail": exc.detail, "object_id": exc.object_id},
    )

validation_error_handler async

validation_error_handler(_request: Request, exc: ValidationError) -> JSONResponse

Handle ValidationError exceptions.

Parameters:

Name Type Description Default
_request Request

The request object.

required
exc ValidationError

The ValidationError exception.

required

Returns:

Type Description
JSONResponse

A JSON response with the error details.

Source code in src/tool_inventory/main.py
@app.exception_handler(ValidationError)
async def validation_error_handler(
    _request: Request,
    exc: ValidationError,
) -> JSONResponse:
    """Handle ValidationError exceptions.

    Args:
        _request: The request object.
        exc: The ValidationError exception.

    Returns:
        A JSON response with the error details.
    """
    return JSONResponse(
        status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
        content={"detail": exc.errors()},
    )