Context
provide.foundation.errors.context
¶
TODO: Add module docstring.
Classes¶
ErrorContext
¶
Rich error context for diagnostics and monitoring.
Provides a flexible container for error metadata that can be used by different systems (logging, monitoring, Terraform, etc.).
Attributes:
| Name | Type | Description |
|---|---|---|
timestamp |
datetime
|
When the error occurred. |
severity |
ErrorSeverity
|
Error severity level. |
category |
ErrorCategory
|
Error category for classification. |
metadata |
dict[str, dict[str, Any]]
|
Namespace-based metadata storage. |
tags |
set[str]
|
Set of tags for categorization and filtering. |
trace_id |
str | None
|
Optional trace ID for distributed tracing. |
span_id |
str | None
|
Optional span ID for distributed tracing. |
Examples:
>>> ctx = ErrorContext(severity=ErrorSeverity.HIGH)
>>> ctx.add_namespace("aws", {"region": "us-east-1", "account": "123456"})
>>> ctx.add_tag("production")
Functions¶
add_namespace
¶
Add namespaced metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
Namespace key (e.g., 'terraform', 'aws', 'http'). |
required |
data
|
dict[str, Any]
|
Metadata for this namespace. |
required |
Returns:
| Type | Description |
|---|---|
ErrorContext
|
Self for method chaining. |
Examples:
>>> ctx.add_namespace("terraform", {"provider": "aws", "version": "5.0"})
>>> ctx.add_namespace("http", {"method": POST, "status": 500})
Source code in provide/foundation/errors/context.py
add_tag
¶
Add a tag for categorization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
str
|
Tag to add. |
required |
Returns:
| Type | Description |
|---|---|
ErrorContext
|
Self for method chaining. |
add_tags
¶
Add multiple tags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*tags
|
str
|
Tags to add. |
()
|
Returns:
| Type | Description |
|---|---|
ErrorContext
|
Self for method chaining. |
get_namespace
¶
to_dict
¶
Convert to dictionary for logging and serialization.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Flattened dictionary with namespaced keys. |
Examples:
Source code in provide/foundation/errors/context.py
to_logging_context
¶
to_terraform_diagnostic
¶
Convert to Terraform diagnostic format.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary formatted for Terraform diagnostics. |
Examples:
Source code in provide/foundation/errors/context.py
update_namespace
¶
Update existing namespace metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
Namespace key to update. |
required |
data
|
dict[str, Any]
|
Metadata to merge into namespace. |
required |
Returns:
| Type | Description |
|---|---|
ErrorContext
|
Self for method chaining. |
Source code in provide/foundation/errors/context.py
Functions¶
capture_error_context
¶
capture_error_context(
error: Exception,
severity: ErrorSeverity | None = None,
category: ErrorCategory | None = None,
**namespaces: dict[str, Any]
) -> ErrorContext
Capture error context from an exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
Exception
|
Exception to capture context from. |
required |
severity
|
ErrorSeverity | None
|
Optional severity override. |
None
|
category
|
ErrorCategory | None
|
Optional category override. |
None
|
**namespaces
|
dict[str, Any]
|
Namespace data to add to context. |
{}
|
Returns:
| Type | Description |
|---|---|
ErrorContext
|
ErrorContext with captured information. |
Examples:
>>> try:
... risky_operation()
... except Exception as e:
... ctx = capture_error_context(
... e,
... severity=ErrorSeverity.HIGH,
... aws={"region": "us-east-1"},
... http={"status": 500}
... )