Improve error messages produced by http decorators #9

Merged
h7x4 merged 1 commits from better-error-messages-for-http-decorators into master 2024-05-17 23:39:59 +02:00
1 changed files with 50 additions and 18 deletions
Showing only changes of commit 2106acaffa - Show all commits

View File

@ -11,43 +11,75 @@ def set_endpoint(base_url:str):
# Exceptions: # Exceptions:
class APIError(Exception): pass class APIError(Exception): pass
def parse_message(method, url, status, function_name, json_text):
prefix = f"[{function_name}] {method} /{url} -> {status}:"
try:
data = json.loads(json_text)
except json.JSONDecodeError:
raise APIError(f"{prefix} Expected json response, got:\n{json_text}")
if "error" not in data:
raise APIError(f"{prefix} Missing json data 'error', got:\n{json_text}")
if data["error"] != False:
raise APIError(f"{prefix} Got error {str(data['error'])}, got:\n{json_text}")
if "success" not in data:
raise APIError(f"{prefix} Missing json data 'error', got:\n{json_text}")
return data
# decorator: # decorator:
# (TODO): Add logging # (TODO): Add logging
def request_delete(func): def request_delete(func):
@wraps(func) @wraps(func)
def new_func(*args, **kwargs): def new_func(*args, **kwargs):
url, data = func(*args, **kwargs) url, data = func(*args, **kwargs)
if type(data) is dict: data = json.dumps(data) response = requests.delete(f"{BASE_URL}/{url}", json=data)
response = requests.delete(f"{BASE_URL}/{url}", data=data) response.raise_for_status() # raises HTTPError, if any
response.raise_for_status() # raises HTTPError of any data = parse_message(
data = json.loads(response.text) "DELETE",
if "error" not in data or data["error"] != False: url,
print(data) response.status_code,
raise APIError(data["error"]) func.__name__,
response.text,
)
return data["success"] return data["success"]
return new_func return new_func
def request_post(func): def request_post(func):
@wraps(func) @wraps(func)
def new_func(*args, **kwargs): def new_func(*args, **kwargs):
url, data = func(*args, **kwargs) url, data = func(*args, **kwargs)
if type(data) is dict: data = json.dumps(data) response = requests.post(f"{BASE_URL}/{url}", json=data)
response = requests.post(f"{BASE_URL}/{url}", data=data) response.raise_for_status() # raises HTTPError, if any
response.raise_for_status() # raises HTTPError of any data = parse_message(
data = json.loads(response.text) "POST",
if "error" not in data or data["error"] != False: url,
print(data) response.status_code,
raise APIError(data["error"]) func.__name__,
response.text,
)
return data["success"] return data["success"]
return new_func return new_func
def request_get(func): def request_get(func):
@wraps(func) @wraps(func)
def new_func(*args, **kwargs): def new_func(*args, **kwargs):
url = func(*args, **kwargs) url = func(*args, **kwargs)
response = requests.get(f"{BASE_URL}/{url}") response = requests.get(f"{BASE_URL}/{url}")
response.raise_for_status() # raises HTTPError of any response.raise_for_status() # raises HTTPError, if any
data = json.loads(response.text) data = parse_message(
if "error" not in data or data["error"] != False: "GET",
raise APIError(data["errortext"]) url,
response.status_code,
func.__name__,
response.text,
)
if "value" not in data:
raise APIError(f"[{func.__name__}] Missing json data 'value', got:\n{json.dumps(data)}")
return data["value"] return data["value"]
return new_func return new_func