-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy patherror_handling.py
More file actions
51 lines (41 loc) · 1.24 KB
/
Copy patherror_handling.py
File metadata and controls
51 lines (41 loc) · 1.24 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
"""Error handling examples for primp."""
import json
import primp
client = primp.Client(impersonate="chrome_146", timeout=10)
# HTTP status errors (4xx/5xx)
try:
resp = client.get("https://httpbin.org/status/404")
except primp.StatusError as e:
print(f"HTTP status error: {e}")
# raise_for_status
resp = client.get("https://httpbin.org/status/200")
try:
resp.raise_for_status()
except primp.StatusError as e:
print(f"Status error: {e}")
# Timeout
try:
client.get("https://httpbin.org/delay/15", timeout=2)
except primp.TimeoutError as e:
print(f"Timeout: {e}")
# Read timeout (max gap between bytes)
try:
client.get("https://httpbin.org/delay/10", read_timeout=2)
except primp.TimeoutError as e:
print(f"Read timeout: {e}")
# Connection errors (DNS, proxy, SSL)
try:
client.get("https://nonexistent-domain-12345.com")
except primp.ConnectError as e:
print(f"Connection error: {e}")
# JSON decode errors (standard Python exception, not primp)
try:
resp = client.get("https://httpbin.org/html")
data = resp.json()
except json.JSONDecodeError as e:
print(f"JSON decode error: {e}")
# Catch-all
try:
client.get("https://httpbin.org/status/500")
except primp.PrimpError as e:
print(f"Request failed: {e}")