Exceptions¶
TenancyError ¶
Bases: Exception
Base exception for all fastapi-tenancy errors.
Every public exception in this library subclasses TenancyError,
enabling callers to distinguish tenancy-specific failures from other
application exceptions with a single except clause.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Human-readable description of the error. |
|
details |
dict[str, Any]
|
Supplementary key-value context. Safe to log; must never contain raw secrets, full stack traces, or user PII. |
Source code in src/fastapi_tenancy/core/exceptions.py
TenantNotFoundError ¶
Bases: TenancyError
Raised when a tenant cannot be located in the configured store.
Typical causes
- The identifier extracted from the request matches no known tenant.
- A tenant was deleted between resolution and usage.
- The store is empty (e.g. first run without seed data).
Attributes:
| Name | Type | Description |
|---|---|---|
identifier |
The identifier that was looked up ( |
Source code in src/fastapi_tenancy/core/exceptions.py
TenantResolutionError ¶
TenantResolutionError(
reason: str,
strategy: str | None = None,
details: dict[str, Any] | None = None,
)
Bases: TenancyError
Raised when the configured strategy cannot extract a tenant from a request.
This is distinct from TenantNotFoundError:
- Resolution failure → the request itself is malformed (missing header, bad JWT format, wrong path prefix, etc.)
- Not-found → the identifier was valid but the tenant does not exist.
Attributes:
| Name | Type | Description |
|---|---|---|
reason |
A concise, operator-readable explanation of why resolution failed. Never include raw user input in this field. |
|
strategy |
The resolution strategy that was active (e.g. |
Source code in src/fastapi_tenancy/core/exceptions.py
| Python | |
|---|---|
TenantInactiveError ¶
Bases: TenancyError
Raised when a resolved tenant is not in the ACTIVE status.
Prevents suspended, deleted, or provisioning tenants from accessing any resource behind the tenancy middleware.
Attributes:
| Name | Type | Description |
|---|---|---|
tenant_id |
Internal ID of the inactive tenant. |
|
status |
The tenant's current lifecycle status value. |
Source code in src/fastapi_tenancy/core/exceptions.py
IsolationError ¶
IsolationError(
operation: str,
tenant_id: str | None = None,
details: dict[str, Any] | None = None,
)
Bases: TenancyError
Raised when a data-isolation operation fails.
Isolation errors are serious operational failures — they indicate that the library cannot guarantee tenant data separation. Operators should treat these as high-severity alerts requiring immediate investigation.
Attributes:
| Name | Type | Description |
|---|---|---|
operation |
The isolation operation that failed (e.g. |
|
tenant_id |
The affected tenant's ID ( |
Source code in src/fastapi_tenancy/core/exceptions.py
ConfigurationError ¶
Bases: TenancyError
Raised when TenancyConfig contains an invalid or inconsistent value.
Raised at construction time so misconfigured applications fail fast during startup rather than silently producing wrong behaviour at the first request.
Attributes:
| Name | Type | Description |
|---|---|---|
parameter |
The name of the invalid configuration field. |
|
reason |
Why the current value is invalid. |
Source code in src/fastapi_tenancy/core/exceptions.py
MigrationError ¶
MigrationError(
tenant_id: str,
operation: str,
reason: str,
details: dict[str, Any] | None = None,
)
Bases: TenancyError
Raised when an Alembic migration operation fails for a tenant.
Attributes:
| Name | Type | Description |
|---|---|---|
tenant_id |
The affected tenant's ID. |
|
operation |
The migration operation (e.g. |
|
reason |
The underlying error description. |
Source code in src/fastapi_tenancy/core/exceptions.py
| Python | |
|---|---|
RateLimitExceededError ¶
RateLimitExceededError(
tenant_id: str,
limit: int,
window_seconds: int,
details: dict[str, Any] | None = None,
)
Bases: TenancyError
Raised when a tenant exceeds its configured request rate limit.
Attributes:
| Name | Type | Description |
|---|---|---|
tenant_id |
The rate-limited tenant's ID. |
|
limit |
The threshold that was exceeded (requests per window). |
|
window_seconds |
Duration of the rate-limit window in seconds. |
Source code in src/fastapi_tenancy/core/exceptions.py
TenantDataLeakageError ¶
TenantDataLeakageError(
operation: str,
expected_tenant: str,
actual_tenant: str,
details: dict[str, Any] | None = None,
)
Bases: TenancyError
Raised when a potential cross-tenant data leakage is detected.
This is a critical security exception. Any occurrence should trigger an immediate alert, halt the request, and trigger incident-response.
Attributes:
| Name | Type | Description |
|---|---|---|
operation |
The operation during which leakage was detected. |
|
expected_tenant |
The tenant that should own the data. |
|
actual_tenant |
The tenant whose data was encountered instead. |
Source code in src/fastapi_tenancy/core/exceptions.py
TenantQuotaExceededError ¶
TenantQuotaExceededError(
tenant_id: str,
quota_type: str,
current: int | float,
limit: int | float,
details: dict[str, Any] | None = None,
)
Bases: TenancyError
Raised when a tenant exceeds a resource quota.
Attributes:
| Name | Type | Description |
|---|---|---|
tenant_id |
The affected tenant's ID. |
|
quota_type |
The type of quota exceeded (e.g. |
|
current |
Current usage level. |
|
limit |
The configured quota limit. |
Source code in src/fastapi_tenancy/core/exceptions.py
DatabaseConnectionError ¶
DatabaseConnectionError(
tenant_id: str,
reason: str,
details: dict[str, Any] | None = None,
)
Bases: TenancyError
Raised when the library cannot establish a database connection for a tenant.
Most common in DATABASE isolation mode where each tenant has a dedicated
database that may be temporarily unavailable.
Attributes:
| Name | Type | Description |
|---|---|---|
tenant_id |
The affected tenant's ID. |
|
reason |
A concise description of the connection failure. |