Improve NPMplus HTTP error details
This commit is contained in:
@@ -48,7 +48,17 @@ def _request_json(
|
|||||||
with urlopen(req, timeout=10, context=context) as resp:
|
with urlopen(req, timeout=10, context=context) as resp:
|
||||||
payload = resp.read().decode("utf-8")
|
payload = resp.read().decode("utf-8")
|
||||||
except HTTPError as e:
|
except HTTPError as e:
|
||||||
raise RuntimeError(f"HTTP {e.code}") from e
|
detail = f"HTTP {e.code}"
|
||||||
|
try:
|
||||||
|
payload = e.read().decode("utf-8").strip()
|
||||||
|
except Exception:
|
||||||
|
payload = ""
|
||||||
|
if payload:
|
||||||
|
payload = " ".join(payload.split())
|
||||||
|
if len(payload) > 300:
|
||||||
|
payload = payload[:300] + "..."
|
||||||
|
detail = f"{detail}: {payload}"
|
||||||
|
raise RuntimeError(f"{detail} ({url})") from e
|
||||||
except URLError as e:
|
except URLError as e:
|
||||||
raise RuntimeError(str(e.reason)) from e
|
raise RuntimeError(str(e.reason)) from e
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user