Exceptions¶
Exception Hierarchy¶
Varlord provides a unified exception hierarchy for error handling. All custom exceptions inherit from VarlordError.
Base Exception¶
- class varlord.exceptions.VarlordError(message, code=None)[source]¶
Bases:
ExceptionBase exception for all Varlord errors.
All exceptions raised by Varlord inherit from this class, allowing users to: - Catch all Varlord errors with
except VarlordError- Identify error types programmatically using thecodeattribute- message¶
Human-readable error message
- code¶
Machine-readable error code (e.g., “CONFIG_LOAD_FAILED”)
Example
>>> try: ... cfg.load() ... except VarlordError as e: ... print(f"Error {e.code}: {e.message}")
Configuration Exceptions¶
- class varlord.exceptions.ConfigError(message, code=None)[source]¶
Bases:
VarlordErrorBase exception for configuration-related errors.
- class varlord.exceptions.ConfigLoadError(message, source_name=None, code='CONFIG_LOAD_FAILED')[source]¶
Bases:
ConfigErrorRaised when configuration loading fails.
This can occur when: - A source fails to load (e.g., file not found, permission denied) - Type conversion fails (e.g., invalid int value) - Validation fails (e.g., missing required field)
- source_name¶
Name of the source that failed (if applicable)
- class varlord.exceptions.SourceLoadError(message, source_name=None, code='CONFIG_LOAD_FAILED')[source]¶
Bases:
ConfigLoadErrorRaised when a configuration source fails to load.
This is a subclass of
ConfigLoadErrorfor backward compatibility.
Validation Exceptions¶
- class varlord.exceptions.ValidationError(message, field_name=None, code='VALIDATION_FAILED')[source]¶
Bases:
ConfigErrorRaised when configuration validation fails.
This can occur when: - Required fields are missing - Field values fail custom validation - Model definition is invalid
- class varlord.exceptions.RequiredFieldError(message, field_name=None)[source]¶
Bases:
ValidationErrorRaised when a required field is missing.
- field_name¶
Name of the missing required field
- class varlord.exceptions.ModelDefinitionError(message)[source]¶
Bases:
ValidationErrorRaised when model definition is invalid.
This can occur when: - Fields are missing required metadata - Field types are unsupported - Field names conflict with reserved names
- class varlord.exceptions.ConversionError(message, field_name=None, field_type=None, value=None)[source]¶
Bases:
ConfigErrorRaised when type conversion fails.
This can occur when: - String value cannot be converted to target type - Invalid boolean value (not “true”/”false”/”1”/”0”) - Invalid numeric value (not an integer/float)
- class varlord.exceptions.ResolverError(message, code='RESOLVER_FAILED')[source]¶
Bases:
ConfigErrorRaised when source resolution fails.
This can occur when: - Priority policy is invalid - Source merge fails - Circular dependencies detected
Usage Examples¶
Catching All Varlord Exceptions¶
from varlord import Config, VarlordError
cfg = Config(model=MyConfig, sources=[...])
try:
config = cfg.load()
except VarlordError as e:
print(f"Configuration error {e.code}: {e.message}")
Catching Specific Exceptions¶
from varlord import Config, ConfigLoadError, ValidationError
cfg = Config(model=MyConfig, sources=[...])
try:
config = cfg.load()
except ConfigLoadError as e:
print(f"Failed to load from {e.source_name}: {e.message}")
except ValidationError as e:
print(f"Field {e.field_name} failed validation: {e.message}")
Error Codes¶
All exceptions have an error code for programmatic handling:
CONFIG_LOAD_FAILED- Configuration loading failedSOURCE_LOAD_FAILED- Source failed to loadVALIDATION_FAILED- Validation failedMISSING_REQUIRED_FIELD- Required field is missingINVALID_MODEL_DEFINITION- Model definition is invalidTYPE_CONVERSION_FAILED- Type conversion failedRESOLVER_FAILED- Source resolution failed
Note
Always catch VarlordError instead of generic Exception to distinguish configuration errors from other exceptions.
See also
ConfigMain configuration class
varlord.sourcesConfiguration sources