Skip to content

Commit 21576a8

Browse files
fix: Handle ApiException status type checking errors
- Add proper type checking for ApiException.status attribute - Default to status code 500 when status is not an integer - Fixes ty type checker errors in agent.py, organizations.py, and users.py
1 parent 7cb9ab2 commit 21576a8

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/codegen/agents/agent.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ def refresh(self) -> None:
4848
self.status = job_dict.get("status")
4949
self.result = job_dict.get("result")
5050
except ApiException as e:
51-
error = handle_api_error(e.status, str(e), getattr(e, "body", None))
51+
status_code = getattr(e, "status", None)
52+
if not isinstance(status_code, int):
53+
status_code = 500 # Default to server error if status is not available
54+
error = handle_api_error(status_code, str(e), getattr(e, "body", None))
5255
raise error from e
5356

5457
def is_completed(self) -> bool:
@@ -116,7 +119,10 @@ def run(self, prompt: str) -> AgentTask:
116119
self.current_job = job
117120
return job
118121
except ApiException as e:
119-
error = handle_api_error(e.status, str(e), getattr(e, "body", None))
122+
status_code = getattr(e, "status", None)
123+
if not isinstance(status_code, int):
124+
status_code = 500 # Default to server error if status is not available
125+
error = handle_api_error(status_code, str(e), getattr(e, "body", None))
120126
raise error from e
121127

122128
def get_status(self) -> dict[str, Any] | None:
@@ -150,5 +156,8 @@ def get_task(self, task_id: int | str) -> AgentTask:
150156
response = self.agents_api.get_agent_run_v1_organizations_org_id_agent_run_agent_run_id_get(org_id=int(self.org_id), agent_run_id=int(task_id), authorization=f"Bearer {self.token}")
151157
return AgentTask(response, self.api_client, self.org_id)
152158
except ApiException as e:
153-
error = handle_api_error(e.status, str(e), getattr(e, "body", None))
159+
status_code = getattr(e, "status", None)
160+
if not isinstance(status_code, int):
161+
status_code = 500 # Default to server error if status is not available
162+
error = handle_api_error(status_code, str(e), getattr(e, "body", None))
154163
raise error from e

src/codegen/organizations/organizations.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ def list(self, limit: int = 50, skip: int = 0) -> list[Organization]:
8080
)
8181
return [Organization(org) for org in response.items]
8282
except ApiException as e:
83-
error = handle_api_error(e.status, str(e), getattr(e, "body", None))
83+
status_code = getattr(e, "status", None)
84+
if not isinstance(status_code, int):
85+
status_code = 500 # Default to server error if status is not available
86+
error = handle_api_error(status_code, str(e), getattr(e, "body", None))
8487
raise error from e
8588

8689
def list_all(self, page_size: int = 50) -> Iterator[Organization]:
@@ -137,5 +140,8 @@ def get_page(self, page: int = 1, page_size: int = 50) -> dict[str, Any]:
137140

138141
return {"items": [Organization(org) for org in response.items], "total": response.total, "page": response.page, "size": response.size, "pages": response.pages}
139142
except ApiException as e:
140-
error = handle_api_error(e.status, str(e), getattr(e, "body", None))
143+
status_code = getattr(e, "status", None)
144+
if not isinstance(status_code, int):
145+
status_code = 500 # Default to server error if status is not available
146+
error = handle_api_error(status_code, str(e), getattr(e, "body", None))
141147
raise error from e

src/codegen/users/users.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ def list(self, limit: int = 50, skip: int = 0) -> list[User]:
9898
)
9999
return [User(user) for user in response.items]
100100
except ApiException as e:
101-
error = handle_api_error(e.status, str(e), getattr(e, "body", None))
101+
status_code = getattr(e, "status", None)
102+
if not isinstance(status_code, int):
103+
status_code = 500 # Default to server error if status is not available
104+
error = handle_api_error(status_code, str(e), getattr(e, "body", None))
102105
raise error from e
103106

104107
def list_all(self, page_size: int = 50) -> Iterator[User]:
@@ -145,7 +148,10 @@ def get(self, user_id: int | str) -> User:
145148
response = self.users_api.get_user_v1_organizations_org_id_users_user_id_get(org_id=self.org_id, user_id=str(user_id), authorization=f"Bearer {self.token}")
146149
return User(response)
147150
except ApiException as e:
148-
error = handle_api_error(e.status, str(e), getattr(e, "body", None))
151+
status_code = getattr(e, "status", None)
152+
if not isinstance(status_code, int):
153+
status_code = 500 # Default to server error if status is not available
154+
error = handle_api_error(status_code, str(e), getattr(e, "body", None))
149155
raise error from e
150156

151157
def get_page(self, page: int = 1, page_size: int = 50) -> dict[str, Any]:
@@ -174,7 +180,10 @@ def get_page(self, page: int = 1, page_size: int = 50) -> dict[str, Any]:
174180

175181
return {"items": [User(user) for user in response.items], "total": response.total, "page": response.page, "size": response.size, "pages": response.pages}
176182
except ApiException as e:
177-
error = handle_api_error(e.status, str(e), getattr(e, "body", None))
183+
status_code = getattr(e, "status", None)
184+
if not isinstance(status_code, int):
185+
status_code = 500 # Default to server error if status is not available
186+
error = handle_api_error(status_code, str(e), getattr(e, "body", None))
178187
raise error from e
179188

180189
def find_by_github_username(self, github_username: str) -> User | None:

0 commit comments

Comments
 (0)