error_policy
error_policy
Error handling policy for pyetm models.
This module defines how warnings and errors are handled based on: 1. Error mode (safe/default/dangerous) from PYETM_ERROR_MODE env var 2. Warning severity (error/warning/info) 3. Execution context (single operation vs bulk operation)
Error Modes
- safe: All warnings raise exceptions (maximum safety for CI/production)
- default: Only severity="error" warnings raise in single operations (smart behavior)
- dangerous: No warnings raise, all are logged (useful for exploratory analysis)
Examples:
>>> # Single operation, default mode
>>> scenario.update(inputs={"bad_key": 123}) # Raises if validation fails
>>> # Bulk operation, default mode
>>> scenarios.create_many([...]) # Collects warnings, continues processing
Classes
ErrorMode
Bases: str, Enum
Error handling mode for pyetm operations.
ErrorPolicy
Determines whether warnings should raise exceptions based on context and mode.
Initialize error policy with specified mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
ErrorMode
|
Error handling mode (safe/default/dangerous) |
DEFAULT
|
Source code in src/pyetm/models/error_policy.py
Functions
should_raise
Determine if a warning should raise an exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
severity
|
Literal['info', 'warning', 'error']
|
Warning severity level |
required |
is_bulk_context
|
bool
|
True if warning occurred during bulk operation |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if warning should raise an exception, False to log only |
Decision Matrix
SAFE mode: Always raise (all severities, all contexts) DANGEROUS mode: Never raise (all severities, all contexts) DEFAULT mode: - Single operations: Raise on severity="error" - Bulk operations: Never raise (collect warnings)
Source code in src/pyetm/models/error_policy.py
Functions
get_error_policy
cached
Get singleton ErrorPolicy instance based on current settings.
Returns:
| Type | Description |
|---|---|
ErrorPolicy
|
ErrorPolicy configured according to PYETM_ERROR_MODE env var |
Note
This function is cached to avoid repeated settings imports. Clear cache with get_error_policy.cache_clear() if settings change.