-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handling.py
More file actions
261 lines (219 loc) · 8.61 KB
/
error_handling.py
File metadata and controls
261 lines (219 loc) · 8.61 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
"""Error handling example for the ZipTax SDK."""
from ziptax import (
ZipTaxClient,
ZipTaxError,
ZipTaxAPIError,
ZipTaxAuthenticationError,
ZipTaxAuthorizationError,
ZipTaxNotFoundError,
ZipTaxRateLimitError,
ZipTaxServerError,
ZipTaxValidationError,
ZipTaxConnectionError,
ZipTaxTimeoutError,
ZipTaxRetryError,
)
def example_validation_errors():
"""Example of handling validation errors."""
print("=" * 60)
print("Example 1: Validation Errors")
print("=" * 60)
try:
# This will fail due to invalid API key
client = ZipTaxClient.api_key("short")
except ZipTaxValidationError as e:
print(f"Validation Error: {e.message}")
print("Fix: Provide a valid API key")
client = ZipTaxClient.api_key("valid-api-key-1234567890")
try:
# This will fail due to empty address
response = client.request.GetSalesTaxByAddress("")
except ZipTaxValidationError as e:
print(f"\nValidation Error: {e.message}")
print("Fix: Provide a non-empty address")
try:
# This will fail due to address being too long
long_address = "a" * 101
response = client.request.GetSalesTaxByAddress(long_address)
except ZipTaxValidationError as e:
print(f"\nValidation Error: {e.message}")
print("Fix: Limit address to 100 characters")
try:
# This will fail due to invalid country code
response = client.request.GetSalesTaxByAddress(
"123 Main St",
country_code="INVALID"
)
except ZipTaxValidationError as e:
print(f"\nValidation Error: {e.message}")
print("Fix: Use 'USA' or 'CAN' as country code")
try:
# This will fail due to invalid historical date format
response = client.request.GetSalesTaxByAddress(
"123 Main St",
historical="2024-13-01"
)
except ZipTaxValidationError as e:
print(f"\nValidation Error: {e.message}")
print("Fix: Use YYYYMM format (e.g., '202401')")
try:
# This will fail due to invalid coordinates
response = client.request.GetSalesTaxByGeoLocation(
lat="invalid",
lng="-117.8386"
)
except ZipTaxValidationError as e:
print(f"\nValidation Error: {e.message}")
print("Fix: Provide valid numeric coordinates")
client.close()
def example_api_errors():
"""Example of handling API errors."""
print("\n" + "=" * 60)
print("Example 2: API Errors")
print("=" * 60)
# Authentication Error (401)
try:
client = ZipTaxClient.api_key("invalid-api-key-1234567890")
response = client.request.GetSalesTaxByAddress("123 Main St")
except ZipTaxAuthenticationError as e:
print(f"Authentication Error: {e.message}")
print(f"Status Code: {e.status_code}")
print("Fix: Check your API key is correct")
except Exception as e:
print(f"Note: This example requires a real API call to trigger: {type(e).__name__}")
# Authorization Error (403)
try:
# This would happen if API key is valid but lacks permissions
client = ZipTaxClient.api_key("valid-but-unauthorized-key")
response = client.request.GetAccountMetrics()
except ZipTaxAuthorizationError as e:
print(f"\nAuthorization Error: {e.message}")
print(f"Status Code: {e.status_code}")
print("Fix: Check your API key has the required permissions")
except Exception as e:
print(f"Note: This example requires a real API call to trigger: {type(e).__name__}")
# Rate Limit Error (429)
try:
# This would happen if you exceed rate limits
client = ZipTaxClient.api_key("your-api-key-here")
# Make many rapid requests...
for i in range(1000):
response = client.request.GetSalesTaxByAddress(f"{i} Main St")
except ZipTaxRateLimitError as e:
print(f"\nRate Limit Error: {e.message}")
print(f"Status Code: {e.status_code}")
if e.retry_after:
print(f"Retry After: {e.retry_after} seconds")
print("Fix: Implement rate limiting or wait before retrying")
except Exception as e:
print(f"Note: This example requires hitting rate limits: {type(e).__name__}")
def example_connection_errors():
"""Example of handling connection errors."""
print("\n" + "=" * 60)
print("Example 3: Connection Errors")
print("=" * 60)
# Timeout Error
try:
client = ZipTaxClient.api_key(
"your-api-key-here",
timeout=0.001 # Very short timeout to trigger error
)
response = client.request.GetSalesTaxByAddress("123 Main St")
except ZipTaxTimeoutError as e:
print(f"Timeout Error: {e.message}")
print("Fix: Increase timeout or check network connection")
except Exception as e:
print(f"Note: This example requires a real API call: {type(e).__name__}")
# Connection Error
try:
client = ZipTaxClient.api_key(
"your-api-key-here",
base_url="https://invalid-domain-that-does-not-exist.com"
)
response = client.request.GetSalesTaxByAddress("123 Main St")
except ZipTaxConnectionError as e:
print(f"\nConnection Error: {e.message}")
print("Fix: Check base URL and network connection")
except Exception as e:
print(f"Note: This example requires a real API call: {type(e).__name__}")
def example_retry_errors():
"""Example of handling retry errors."""
print("\n" + "=" * 60)
print("Example 4: Retry Errors")
print("=" * 60)
try:
# Configure client with limited retries
client = ZipTaxClient.api_key(
"your-api-key-here",
max_retries=2,
retry_delay=0.5
)
# This would fail after exhausting retries (if server errors occur)
response = client.request.GetSalesTaxByAddress("123 Main St")
except ZipTaxRetryError as e:
print(f"Retry Error: {e.message}")
print(f"Attempts: {e.attempts}")
if e.last_exception:
print(f"Last Exception: {type(e.last_exception).__name__}")
print("Fix: Check server status or increase max_retries")
except Exception as e:
print(f"Note: This example requires server errors: {type(e).__name__}")
def example_comprehensive_error_handling():
"""Example of comprehensive error handling."""
print("\n" + "=" * 60)
print("Example 5: Comprehensive Error Handling")
print("=" * 60)
client = ZipTaxClient.api_key("your-api-key-here")
try:
response = client.request.GetSalesTaxByAddress(
"200 Spectrum Center Drive, Irvine, CA 92618"
)
print(f"Success! Address: {response.address_detail.normalized_address}")
if response.tax_summaries:
rate = response.tax_summaries[0].rate
print(f"Tax rate: {rate * 100:.2f}%")
except ZipTaxValidationError as e:
print(f"Validation Error: {e.message}")
print("Action: Fix input parameters")
except ZipTaxAuthenticationError as e:
print(f"Authentication Error: {e.message}")
print("Action: Verify API key")
except ZipTaxRateLimitError as e:
print(f"Rate Limit Error: {e.message}")
if e.retry_after:
print(f"Action: Wait {e.retry_after} seconds before retrying")
else:
print("Action: Implement exponential backoff")
except ZipTaxServerError as e:
print(f"Server Error: {e.message}")
print(f"Status Code: {e.status_code}")
print("Action: Retry later or contact support")
except ZipTaxConnectionError as e:
print(f"Connection Error: {e.message}")
print("Action: Check network connection")
except ZipTaxTimeoutError as e:
print(f"Timeout Error: {e.message}")
print("Action: Increase timeout or check network")
except ZipTaxAPIError as e:
print(f"API Error: {e.message}")
print(f"Status Code: {e.status_code}")
print("Action: Review API documentation")
except ZipTaxError as e:
print(f"General ZipTax Error: {e.message}")
print("Action: Review error details")
except Exception as e:
print(f"Unexpected Error: {e}")
print("Action: Report to support")
finally:
client.close()
print("\nClient closed successfully")
if __name__ == "__main__":
# Run all examples
example_validation_errors()
example_api_errors()
example_connection_errors()
example_retry_errors()
example_comprehensive_error_handling()
print("\n" + "=" * 60)
print("Error Handling Examples Complete")
print("=" * 60)