Handlers
provide.foundation.errors.handlers
¶
TODO: Add module docstring.
Classes¶
ErrorHandler
¶
Configurable error handler with type-based policies.
Attributes:
| Name | Type | Description |
|---|---|---|
policies |
dict[type[Exception], Callable[[Exception], Any]]
|
Mapping of error types to handler functions. |
default_action |
Callable[[Exception], Any]
|
Default handler for unmatched errors. |
log_all |
bool
|
Whether to log all handled errors. |
capture_context |
bool
|
Whether to capture error context. |
reraise_unhandled |
bool
|
Whether to re-raise unhandled errors. |
Examples:
>>> def handle_validation(e: ValidationError):
... return {"error": "Invalid input", "details": e.context}
...
>>> handler = ErrorHandler(
... policies={ValidationError: handle_validation},
... default_action=lambda e: None
... )
>>> result = handler.handle(some_error)
Functions¶
add_policy
¶
Add or update a handler policy for an error type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error_type
|
type[Exception]
|
Exception type to handle. |
required |
handler
|
Callable[[Exception], Any]
|
Handler function for this error type. |
required |
Returns:
| Type | Description |
|---|---|
ErrorHandler
|
Self for method chaining. |
Source code in provide/foundation/errors/handlers.py
handle
¶
Handle an error based on configured policies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
Exception
|
The exception to handle. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Result from the handler function. |
Raises:
| Type | Description |
|---|---|
Exception
|
The original error if reraise_unhandled=True and no handler matches. |
Examples:
Source code in provide/foundation/errors/handlers.py
Functions¶
create_error_handler
¶
Create an error handler with policies from keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**policies
|
Callable[[Exception], Any]
|
Error type names mapped to handler functions. |
{}
|
Returns:
| Type | Description |
|---|---|
ErrorHandler
|
Configured ErrorHandler instance. |
Examples:
>>> handler = create_error_handler(
... ValidationError=lambda e: {"error": str(e)},
... NetworkError=lambda e: retry_operation(),
... default=lambda e: None
... )
Source code in provide/foundation/errors/handlers.py
error_boundary
¶
error_boundary(
*catch: type[Exception],
on_error: Callable[[Exception], Any] | None = None,
log_errors: bool = True,
reraise: bool = True,
context: dict[str, Any] | None = None,
fallback: Any = None
) -> Generator[None, None, None]
Context manager for structured error handling with logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*catch
|
type[Exception]
|
Exception types to catch (defaults to Exception if empty). |
()
|
on_error
|
Callable[[Exception], Any] | None
|
Optional callback function when error is caught. |
None
|
log_errors
|
bool
|
Whether to log caught errors. |
True
|
reraise
|
bool
|
Whether to re-raise after handling. |
True
|
context
|
dict[str, Any] | None
|
Additional context for error logging. |
None
|
fallback
|
Any
|
Value to return if error is suppressed (when reraise=False). |
None
|
Yields:
| Type | Description |
|---|---|
None
|
None |
Examples:
>>> # Suppress and log specific errors
>>> with error_boundary(KeyError, reraise=False, fallback=None):
... value = data["missing_key"]
Source code in provide/foundation/errors/handlers.py
handle_error
¶
handle_error(
error: Exception,
*,
log: bool = True,
capture_context: bool = True,
reraise: bool = False,
fallback: Any = None
) -> Any
Handle an error with logging and optional context capture.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
Exception
|
The exception to handle. |
required |
log
|
bool
|
Whether to log the error. |
True
|
capture_context
|
bool
|
Whether to capture error context. |
True
|
reraise
|
bool
|
Whether to re-raise the error after handling. |
False
|
fallback
|
Any
|
Value to return if not re-raising. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The fallback value if not re-raising. |
Raises:
| Type | Description |
|---|---|
Exception
|
The original error if reraise=True. |
Examples:
>>> try:
... risky_operation()
... except ValueError as e:
... result = handle_error(e, fallback="default")
Source code in provide/foundation/errors/handlers.py
transactional
¶
transactional(
rollback: Callable[[], None],
commit: Callable[[], None] | None = None,
on_error: Callable[[Exception], None] | None = None,
log_errors: bool = True,
) -> Generator[None, None, None]
Context manager for transactional operations with rollback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rollback
|
Callable[[], None]
|
Function to call on error to rollback changes. |
required |
commit
|
Callable[[], None] | None
|
Optional function to call on success. |
None
|
on_error
|
Callable[[Exception], None] | None
|
Optional error handler before rollback. |
None
|
log_errors
|
bool
|
Whether to log errors. |
True
|
Yields:
| Type | Description |
|---|---|
None
|
None |
Examples:
>>> def rollback_changes():
... db.rollback()
...
>>> def commit_changes():
... db.commit()
...
>>> with transactional(rollback_changes, commit_changes):
... db.execute("INSERT INTO users ...")
... db.execute("UPDATE accounts ...")