Spaces:
Running
Running
Improve: Better Cloudflare API error diagnostics
Browse files
cloudflare-keepalive-setup.py
CHANGED
|
@@ -9,6 +9,7 @@ import re
|
|
| 9 |
import sys
|
| 10 |
import time
|
| 11 |
import urllib.request
|
|
|
|
| 12 |
from pathlib import Path
|
| 13 |
|
| 14 |
API_BASE = "https://api.cloudflare.com/client/v4"
|
|
@@ -22,8 +23,17 @@ def cf_request(method: str, path: str, token: str, body: bytes | None = None, co
|
|
| 22 |
method=method,
|
| 23 |
headers={"Authorization": f"Bearer {token}", "Content-Type": content_type},
|
| 24 |
)
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
if not payload.get("success"):
|
| 28 |
errors = payload.get("errors") or [{"message": "Unknown Cloudflare API error"}]
|
| 29 |
raise RuntimeError(errors[0].get("message", "Unknown Cloudflare API error"))
|
|
|
|
| 9 |
import sys
|
| 10 |
import time
|
| 11 |
import urllib.request
|
| 12 |
+
import urllib.error
|
| 13 |
from pathlib import Path
|
| 14 |
|
| 15 |
API_BASE = "https://api.cloudflare.com/client/v4"
|
|
|
|
| 23 |
method=method,
|
| 24 |
headers={"Authorization": f"Bearer {token}", "Content-Type": content_type},
|
| 25 |
)
|
| 26 |
+
try:
|
| 27 |
+
with urllib.request.urlopen(req, timeout=30) as response:
|
| 28 |
+
payload = json.loads(response.read().decode("utf-8"))
|
| 29 |
+
except urllib.error.HTTPError as e:
|
| 30 |
+
try:
|
| 31 |
+
error_body = json.loads(e.read().decode("utf-8"))
|
| 32 |
+
errors = error_body.get("errors") or [{"message": "Unknown error"}]
|
| 33 |
+
error_msg = errors[0].get("message", "Unknown error") if errors else "Unknown error"
|
| 34 |
+
except:
|
| 35 |
+
error_msg = f"HTTP {e.code}: {e.reason}"
|
| 36 |
+
raise RuntimeError(f"Cloudflare API {e.code}: {error_msg}")
|
| 37 |
if not payload.get("success"):
|
| 38 |
errors = payload.get("errors") or [{"message": "Unknown Cloudflare API error"}]
|
| 39 |
raise RuntimeError(errors[0].get("message", "Unknown Cloudflare API error"))
|