-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-handling.py
More file actions
executable file
·83 lines (65 loc) · 2.66 KB
/
error-handling.py
File metadata and controls
executable file
·83 lines (65 loc) · 2.66 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
#!/usr/bin/env -S uv run python
"""
Handling polling timeouts and errors.
This example demonstrates:
- How to catch PollTimeoutError specifically
- How to access timeout error details
- How to configure polling parameters
- How to handle different error scenarios
- Best practices for error handling
"""
import os
from checkout_intents import CheckoutIntents, PollTimeoutError, CheckoutIntentsError
def main() -> None:
"""Demonstrate error handling for polling operations."""
client = CheckoutIntents(
api_key=os.getenv("CHECKOUT_INTENTS_API_KEY"),
)
print("Creating checkout intent...")
intent = client.checkout_intents.create(
buyer={
"address1": "123 Main St",
"city": "New York",
"country": "US",
"email": "john.doe@example.com",
"first_name": "John",
"last_name": "Doe",
"phone": "5555555555",
"postal_code": "10001",
"province": "NY",
},
product_url="https://flybyjing.com/collections/shop/products/sichuan-chili-crisp",
quantity=1,
)
print(f"Created checkout intent: {intent.id}")
try:
print("\nPolling with immediate timeout (will timeout)...")
# Poll with immediate timeout to demonstrate error handling
client.checkout_intents.poll_until_awaiting_confirmation(
intent.id,
poll_interval=0,
max_attempts=1,
)
print("✓ Polling succeeded (unexpected)")
except PollTimeoutError as e:
print("✗ Polling timed out as expected")
print(f"\nPollTimeoutError details:")
print(f" Intent ID: {e.intent_id}")
print(f" Attempts made: {e.attempts}")
print(f" Time elapsed: {e.attempts * e.poll_interval}s")
print(f" Poll interval: {e.poll_interval}s")
print(f" Max attempts: {e.max_attempts}")
print(f"\nError message:\n {e}")
print("\nTo fix this:")
print(" - Increase max_attempts (e.g., 60 for ~5 minutes with 5s interval)")
print(" - Increase poll_interval if you want less frequent checks")
print(" - Use appropriate polling method (poll_until_awaiting_confirmation or poll_until_completed)")
# You can also retrieve the intent manually to check its current state
print(f"\nManually checking intent state...")
current_intent = client.checkout_intents.retrieve(e.intent_id)
print(f" Current state: {current_intent.state}")
except CheckoutIntentsError as e:
# Catch other SDK errors (API errors, network errors, etc.)
print(f"✗ Other error: {e}")
if __name__ == "__main__":
main()