-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_error_handling.py
More file actions
102 lines (77 loc) · 3.25 KB
/
07_error_handling.py
File metadata and controls
102 lines (77 loc) · 3.25 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
"""
Example 7: Error Handling — gracefully handle API errors.
Shows how to catch and handle each error type:
- AuthenticationError (bad key)
- ValidationError (bad data)
- RateLimitError (too many requests)
- ServerError (transient failures)
- WaveGuardError (catch-all)
Usage:
export WAVEGUARD_API_KEY="your-key"
python 07_error_handling.py
"""
import os
import time
from waveguard import (
WaveGuard,
WaveGuardError,
AuthenticationError,
ValidationError,
RateLimitError,
ServerError,
)
api_key = os.environ.get("WAVEGUARD_API_KEY", "demo")
def scan_with_retry(wg, training, test, max_retries=3):
"""Scan with automatic retry on transient errors."""
for attempt in range(max_retries):
try:
return wg.scan(training=training, test=test)
except AuthenticationError:
print("❌ Bad API key — check your WAVEGUARD_API_KEY")
raise # Don't retry auth errors
except ValidationError as e:
print(f"❌ Data validation failed: {e.detail}")
raise # Don't retry bad data
except RateLimitError:
wait = 2 ** attempt # exponential backoff
print(f"⏳ Rate limited — retrying in {wait}s (attempt {attempt + 1}/{max_retries})")
time.sleep(wait)
except ServerError:
wait = 2 ** attempt
print(f"⚠️ Server error — retrying in {wait}s (attempt {attempt + 1}/{max_retries})")
time.sleep(wait)
except WaveGuardError as e:
print(f"❌ Unexpected error ({e.status_code}): {e.message}")
raise
raise WaveGuardError(f"Failed after {max_retries} retries")
# ── Demo 1: Successful scan ──────────────────────────────────────────────
print("=== Error Handling Examples ===\n")
print("--- Demo 1: Normal scan with retry wrapper ---")
wg = WaveGuard(api_key=api_key)
training = [{"x": 1}, {"x": 2}, {"x": 3}]
test = [{"x": 2}, {"x": 100}]
try:
result = scan_with_retry(wg, training, test)
print(f"✅ Scan complete: {result.summary.anomalies_found} anomalies\n")
except WaveGuardError as e:
print(f"Scan failed: {e.message}\n")
# ── Demo 2: Bad API key ──────────────────────────────────────────────────
print("--- Demo 2: Bad API key ---")
bad_wg = WaveGuard(api_key="definitely-not-a-real-key")
try:
bad_wg.scan(training=training, test=test)
except AuthenticationError as e:
print(f"Caught AuthenticationError: {e.message}")
print(f" HTTP status: {e.status_code}\n")
except WaveGuardError:
print("Caught some other error (API might accept any key in demo mode)\n")
# ── Demo 3: Validation error ────────────────────────────────────────────
print("--- Demo 3: Invalid data ---")
try:
# Empty training array should fail validation
wg.scan(training=[], test=[{"x": 1}])
except ValidationError as e:
print(f"Caught ValidationError: {e.message}\n")
except WaveGuardError as e:
print(f"Caught error: {e.message}\n")
print("Done!")