Decorators
provide.foundation.errors.decorators
¶
TODO: Add module docstring.
Classes¶
ResilientErrorHandler
¶
ResilientErrorHandler(
fallback: Any = None,
log_errors: bool = True,
context_provider: (
Callable[[], dict[str, Any]] | None
) = None,
context: dict[str, Any] | None = None,
error_mapper: (
Callable[[Exception], Exception] | None
) = None,
suppress: tuple[type[Exception], ...] | None = None,
reraise: bool = True,
)
Encapsulates error handling logic for the resilient decorator.
Source code in provide/foundation/errors/decorators.py
Functions¶
build_context
¶
Build logging context from provider and static context.
Source code in provide/foundation/errors/decorators.py
log_error
¶
Log an error with full details.
Source code in provide/foundation/errors/decorators.py
log_suppressed
¶
Log a suppressed error.
Source code in provide/foundation/errors/decorators.py
map_error
¶
Apply error mapping if configured.
The error_mapper is applied to all exceptions, including FoundationError types. This allows translating low-level foundation errors into higher-level, domain-specific exceptions while preserving error handling benefits.
If the original exception is a FoundationError and the mapped exception is also a FoundationError, the rich diagnostic context (code, context, cause) is automatically preserved.
Source code in provide/foundation/errors/decorators.py
process_error
¶
Process an error according to configuration.
Source code in provide/foundation/errors/decorators.py
should_suppress
¶
Functions¶
fallback_on_error
¶
fallback_on_error(
fallback_func: Callable[..., Any],
*exceptions: type[Exception],
log_errors: bool = True
) -> Callable[[F], F]
Decorator to call a fallback function when errors occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fallback_func
|
Callable[..., Any]
|
Function to call when an error occurs. |
required |
*exceptions
|
type[Exception]
|
Specific exception types to handle (all if empty). |
()
|
log_errors
|
bool
|
Whether to log errors before calling fallback. |
True
|
Returns:
| Type | Description |
|---|---|
Callable[[F], F]
|
Decorated function. |
Examples:
>>> def use_cache():
... return cached_value
...
>>> @fallback_on_error(use_cache, NetworkError)
... def fetch_from_api():
... return api_call()
Source code in provide/foundation/errors/decorators.py
resilient
¶
resilient(
func: None = None,
*,
fallback: Any = None,
log_errors: bool = True,
context_provider: (
Callable[[], dict[str, Any]] | None
) = None,
context: dict[str, Any] | None = None,
error_mapper: (
Callable[[Exception], Exception] | None
) = None,
suppress: tuple[type[Exception], ...] | None = None,
reraise: bool = True
) -> Callable[[F], F]
resilient(
func: F | None = None,
*,
fallback: Any = None,
log_errors: bool = True,
context_provider: (
Callable[[], dict[str, Any]] | None
) = None,
context: dict[str, Any] | None = None,
error_mapper: (
Callable[[Exception], Exception] | None
) = None,
suppress: tuple[type[Exception], ...] | None = None,
reraise: bool = True
) -> Callable[[F], F] | F
Decorator for automatic error handling with logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fallback
|
Any
|
Value to return when an error occurs. |
None
|
log_errors
|
bool
|
Whether to log errors. |
True
|
context_provider
|
Callable[[], dict[str, Any]] | None
|
Function that provides additional logging context. |
None
|
context
|
dict[str, Any] | None
|
Static context dict to include in logs (alternative to context_provider). |
None
|
error_mapper
|
Callable[[Exception], Exception] | None
|
Function to transform exceptions before re-raising. |
None
|
suppress
|
tuple[type[Exception], ...] | None
|
Tuple of exception types to suppress (return fallback instead). |
None
|
reraise
|
bool
|
Whether to re-raise exceptions after logging (default: True). |
True
|
Returns:
| Type | Description |
|---|---|
Callable[[F], F] | F
|
Decorated function. |
Note
Preserving Context in error_mapper: When using error_mapper with FoundationError exceptions, the original exception's context dictionary is not automatically transferred to the mapped exception. To preserve rich context, manually copy it:
from provide.foundation.errors import FoundationError @resilient( ... error_mapper=lambda e: ( ... ValidationError( ... str(e), ... context=e.context if isinstance(e, FoundationError) else {} ... ) if isinstance(e, FoundationError) ... else DomainError(str(e)) ... ) ... ) ... def process_data(data): ... # Low-level FoundationError will be mapped to ValidationError ... # with context preserved ... pass
Examples:
>>> @resilient(fallback=None, suppress=(KeyError,))
... def get_value(data, key):
... return data[key]
>>> @resilient(
... context_provider=lambda: {"request_id": get_request_id()}
... )
... def process_request():
... # errors will be logged with request_id
... pass
>>> @resilient(
... reraise=False,
... context={"component": "orchestrator", "method": "run"}
... )
... def run():
... # errors will be logged but not re-raised
... pass
Source code in provide/foundation/errors/decorators.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
suppress_and_log
¶
suppress_and_log(
*exceptions: type[Exception],
fallback: Any = None,
log_level: str = "warning"
) -> Callable[[F], F]
Decorator to suppress specific exceptions and log them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*exceptions
|
type[Exception]
|
Exception types to suppress. |
()
|
fallback
|
Any
|
Value to return when exception is suppressed. |
None
|
log_level
|
str
|
Log level to use ('debug', 'info', 'warning', 'error'). |
'warning'
|
Returns:
| Type | Description |
|---|---|
Callable[[F], F]
|
Decorated function. |
Examples:
>>> @suppress_and_log(KeyError, AttributeError, fallback={})
... def get_nested_value(data):
... return data["key"].attribute