Skip to content

Commit c4901a4

Browse files
[Chore] Log errors from cf-api for better debugging (#283)
* log errors (json, strings) from cf-api for better debugging * log status code on failed cf-api calls * more detailed log error message --------- Co-authored-by: Sarthak Agarwal <[email protected]>
1 parent e10630d commit c4901a4

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

codeflash/api/cfapi.py

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

5674

5775
@lru_cache(maxsize=1)
@@ -165,10 +183,7 @@ def is_github_app_installed_on_repo(owner: str, repo: str) -> bool:
165183
:return: The response object.
166184
"""
167185
response = make_cfapi_request(endpoint=f"/is-github-app-installed?repo={repo}&owner={owner}", method="GET")
168-
if not response.ok or response.text != "true":
169-
logger.error(f"Error: {response.text}")
170-
return False
171-
return True
186+
return response.ok and response.text == "true"
172187

173188

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

0 commit comments

Comments
 (0)