Answer stability round 2: 1 🔴, 0 🟡 (0 misread); read an error answer's body guarded, so one cut short names its status
This commit is contained in:
@@ -104,7 +104,7 @@ def http_json(method: str, url: str, token: str, body: object) -> object:
|
||||
raw = response.read()
|
||||
except urllib.error.HTTPError as failure:
|
||||
with failure:
|
||||
return Failure(f"{request_name} answered {failure.code}: {error_message(failure.read()) or failure.reason}")
|
||||
return Failure(f"{request_name} answered {failure.code}: {error_message(failure) or failure.reason}")
|
||||
except http.client.HTTPException as failure:
|
||||
return Failure(f"{request_name} got a broken answer: {failure!r}")
|
||||
except (OSError, ValueError) as failure:
|
||||
@@ -116,10 +116,11 @@ def http_json(method: str, url: str, token: str, body: object) -> object:
|
||||
return Failure(f"{request_name} answered with something other than JSON")
|
||||
|
||||
|
||||
def error_message(raw: bytes) -> str:
|
||||
def error_message(failure: urllib.error.HTTPError) -> str:
|
||||
"""Mattermost's message in an error answer; empty where the body is cut short, breaks HTTP or holds none."""
|
||||
try:
|
||||
answer = json.loads(raw)
|
||||
except ValueError:
|
||||
answer = json.loads(failure.read())
|
||||
except (http.client.HTTPException, OSError, ValueError):
|
||||
return ""
|
||||
message = answer.get("message") if isinstance(answer, dict) else None
|
||||
return message if isinstance(message, str) else ""
|
||||
|
||||
Reference in New Issue
Block a user