Exceptions¶
Exception hierarchy for dotenvmodel errors.
exceptions ¶
Exception types for dotenvmodel.
DotEnvModelError ¶
Bases: Exception
Base exception for all dotenvmodel errors.
All other dotenvmodel exceptions inherit from this. Catch this if you want to handle any dotenvmodel error in a single except block.
When to catch
- When you want to handle all config errors uniformly
- As a fallback when specific error handling isn't needed
Example
See Also
ValidationError: For validation failures.MissingFieldError: For missing required fields.
ValidationError ¶
ValidationError(
field_name: str,
value: Any,
error_msg: str,
field_type: TypeForm[Any] | None = None,
env_var_name: str | None = None,
)
Bases: DotEnvModelError
Raised when field validation fails.
This is the base class for specific validation errors. It's raised when a field value fails type coercion or constraint validation.
When to catch
- When you want to handle all validation errors (coercion + constraint)
- As a parent class for
MissingFieldError,TypeCoercionError, andConstraintViolationError
Attributes:
| Name | Type | Description |
|---|---|---|
field_name |
Name of the field that failed validation |
|
value |
The value that failed validation |
|
error_msg |
Human-readable error description |
|
field_type |
The expected type (or None) |
|
env_var_name |
The environment variable name for error messages |
Example
See Also
DotEnvModelError: Base exception.TypeCoercionError: Type coercion failures.ConstraintViolationError: Constraint failures.
Source code in dotenvmodel/exceptions.py
MissingFieldError ¶
MissingFieldError(
field_name: str,
field_type: TypeForm[Any] | None = None,
env_var_name: str | None = None,
)
Bases: ValidationError
Raised when a required field is not set.
This error occurs when a required field has no value in any source (environment variables, .env files, or dict).
When to catch
- When you want to provide specific error messages for missing config
- When you want to show which env vars need to be set
Attributes:
| Name | Type | Description |
|---|---|---|
field_name |
Name of the missing field |
|
env_var_name |
The environment variable name that should be set |
|
field_type |
The expected type (or None) |
Example
See Also
ValidationError: Parent class.
Source code in dotenvmodel/exceptions.py
TypeCoercionError ¶
TypeCoercionError(
field_name: str,
value: Any,
error_msg: str,
field_type: TypeForm[Any] | None = None,
env_var_name: str | None = None,
)
Bases: ValidationError
Raised when type coercion fails.
This error occurs when a string value from the environment cannot be converted to the field's target type (e.g., "abc" to int).
When to catch
- When you want to show helpful messages for invalid config values
- When you want to identify which env var has a formatting issue
Attributes:
| Name | Type | Description |
|---|---|---|
field_name |
Name of the field that failed coercion |
|
value |
The string value that couldn't be coerced |
|
error_msg |
Description of why coercion failed |
|
field_type |
The target type |
|
env_var_name |
The environment variable name |
Example
See Also
ValidationError: Parent class.
Source code in dotenvmodel/exceptions.py
ConstraintViolationError ¶
ConstraintViolationError(
field_name: str,
value: Any,
constraint: str,
error_msg: str,
env_var_name: str | None = None,
)
Bases: ValidationError
Raised when a validation constraint is violated.
This error occurs when a value passes type coercion but fails a validation constraint (e.g., port > 65535, string too short).
When to catch
- When you want to show which constraint was violated
- When you want to help users fix invalid config values
Attributes:
| Name | Type | Description |
|---|---|---|
field_name |
Name of the field that violated a constraint |
|
value |
The value that violated the constraint |
|
constraint |
The constraint that was violated (e.g., "ge=1") |
|
error_msg |
Human-readable constraint error |
|
env_var_name |
The environment variable name |
Example
See Also
ValidationError: Parent class.Field: For defining constraints.
Source code in dotenvmodel/exceptions.py
MultipleValidationErrors ¶
Bases: DotEnvModelError
Raised when multiple validation errors occur simultaneously.
When loading a config with multiple invalid fields, all errors are collected and raised together in this exception rather than failing on the first error.
When to catch
- When you want to show all config errors at once (better UX)
- When you want to identify all missing/invalid fields in one pass
Attributes:
| Name | Type | Description |
|---|---|---|
errors |
List of |
Example
See Also
ValidationError: Individual errors.