Skip to content

Commit a7d3d97

Browse files
log errors (json, strings) from cf-api for better debugging
1 parent 840ed9a commit a7d3d97

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

codeflash/api/cfapi.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,29 @@ def make_cfapi_request(
4444
cfapi_headers = {"Authorization": f"Bearer {get_codeflash_api_key()}"}
4545
if extra_headers:
4646
cfapi_headers.update(extra_headers)
47-
if method.upper() == "POST":
48-
json_payload = json.dumps(payload, indent=None, default=pydantic_encoder)
49-
cfapi_headers["Content-Type"] = "application/json"
50-
response = requests.post(url, data=json_payload, headers=cfapi_headers, timeout=60)
51-
else:
52-
response = requests.get(url, headers=cfapi_headers, timeout=60)
53-
return response
47+
try:
48+
if method.upper() == "POST":
49+
json_payload = json.dumps(payload, indent=None, default=pydantic_encoder)
50+
cfapi_headers["Content-Type"] = "application/json"
51+
response = requests.post(url, data=json_payload, headers=cfapi_headers, timeout=60)
52+
else:
53+
response = requests.get(url, headers=cfapi_headers, timeout=60)
54+
response.raise_for_status()
55+
return response # noqa: TRY300
56+
except requests.exceptions.HTTPError:
57+
# response may be either a string or JSON, so we handle both cases
58+
error_message = ""
59+
try:
60+
json_response = response.json()
61+
if "error" in json_response:
62+
error_message = json_response["error"]
63+
elif "message" in json_response:
64+
error_message = json_response["message"]
65+
except (ValueError, TypeError):
66+
error_message = response.text
67+
68+
logger.error(f"Error making request to Codeflash API: {error_message}")
69+
return response
5470

5571

5672
@lru_cache(maxsize=1)
@@ -164,10 +180,7 @@ def is_github_app_installed_on_repo(owner: str, repo: str) -> bool:
164180
:return: The response object.
165181
"""
166182
response = make_cfapi_request(endpoint=f"/is-github-app-installed?repo={repo}&owner={owner}", method="GET")
167-
if not response.ok or response.text != "true":
168-
logger.error(f"Error: {response.text}")
169-
return False
170-
return True
183+
return response.ok and response.text == "true"
171184

172185

173186
def get_blocklisted_functions() -> dict[str, set[str]] | dict[str, Any]:

0 commit comments

Comments
 (0)