-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
109 lines (86 loc) · 3.49 KB
/
main.py
File metadata and controls
109 lines (86 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
"""Error Handling: retry configuration, nullable fields, and error hierarchy.
Demonstrates Python-specific patterns for robust config access:
- RetryConfig for transient failure recovery
- nullable=True for graceful handling of missing values
- Typed exception hierarchy for precise error handling
Run:
python main.py
Requires a running decree server with seeded data (see ../README.md).
"""
from pathlib import Path
from opendecree import (
ConfigClient,
InvalidArgumentError,
NotFoundError,
RetryConfig,
)
def main() -> None:
tenant_id = get_tenant_id()
# --- Custom retry configuration ---
# Default is 3 attempts with exponential backoff.
# Customize for your use case.
retry = RetryConfig(
max_attempts=5,
initial_backoff=0.2,
max_backoff=10.0,
)
with ConfigClient("localhost:9090", subject="error-example", retry=retry) as client:
# --- Nullable reads ---
# Without nullable, missing values raise NotFoundError.
# With nullable=True, they return None instead.
print("=== Nullable reads ===")
value = client.get(tenant_id, "app.name", str, nullable=True)
print(f"app.name (exists): {value!r}")
# set_null makes a field return None with nullable=True.
client.set_null(tenant_id, "app.debug")
value = client.get(tenant_id, "app.debug", str, nullable=True)
print(f"app.debug (after set_null): {value!r}")
# Restore it.
client.set(tenant_id, "app.debug", "false")
print(f"app.debug (restored): {client.get(tenant_id, 'app.debug', bool)!r}")
# --- Error hierarchy ---
print("\n=== Error hierarchy ===")
# NotFoundError — field doesn't exist.
try:
client.get(tenant_id, "nonexistent.field")
except NotFoundError as e:
print(f"NotFoundError: {e}")
# InvalidArgumentError — value fails validation.
try:
client.set(tenant_id, "server.rate_limit", "-1")
except InvalidArgumentError as e:
print(f"InvalidArgumentError: {e}")
# All decree errors share a common base class.
try:
client.get(tenant_id, "nonexistent.field")
except Exception as e:
# In production, catch the specific type you care about.
from opendecree import DecreeError
if isinstance(e, DecreeError):
print(f"DecreeError base: {type(e).__name__}: {e}")
# --- Retry behavior ---
print("\n=== Retry ===")
print(f"Configured: {retry.max_attempts} attempts, "
f"{retry.initial_backoff}s initial backoff, "
f"{retry.max_backoff}s max backoff")
print("Retries are automatic on UNAVAILABLE and DEADLINE_EXCEEDED.")
# To disable retry entirely:
no_retry_client = ConfigClient(
"localhost:9090",
subject="no-retry-example",
retry=None,
)
with no_retry_client:
val = no_retry_client.get(tenant_id, "app.name")
print(f"No-retry read: {val!r}")
def get_tenant_id() -> str:
import os
if v := os.environ.get("TENANT_ID"):
return v
tenant_file = Path(__file__).parent.parent / ".tenant-id"
if tenant_file.exists():
return tenant_file.read_text().strip()
raise SystemExit("Set TENANT_ID env var or run 'make setup' from the examples directory")
if __name__ == "__main__":
main()