|
1 | | -import json |
| 1 | +from json import JSONDecodeError |
2 | 2 | from typing import Any, AsyncIterator, Literal, Optional, TypeVar, Union, overload |
3 | 3 |
|
4 | 4 | import httpx |
@@ -91,13 +91,9 @@ async def delete(self, path: str) -> None: |
91 | 91 |
|
92 | 92 | def _extract_error(self, data: Union[bytes, str], exception: Optional[Exception] = None) -> WorkflowAIError: |
93 | 93 | try: |
94 | | - data_dict = json.loads(data) |
95 | | - if next(iter(data_dict.keys())) == "detail": |
96 | | - data_dict = {"error": {"detail": data_dict["detail"]}} |
97 | | - data = json.dumps(data_dict) |
98 | 94 | res = ErrorResponse.model_validate_json(data) |
99 | 95 | return WorkflowAIError(res.error, task_run_id=res.task_run_id) |
100 | | - except ValidationError: |
| 96 | + except JSONDecodeError: |
101 | 97 | raise WorkflowAIError( |
102 | 98 | error=BaseError( |
103 | 99 | message="Unknown error" if exception is None else str(exception), |
@@ -129,5 +125,25 @@ async def stream( |
129 | 125 | raise self._extract_error(payload, e) from None |
130 | 126 |
|
131 | 127 | async def raise_for_status(self, response: httpx.Response): |
132 | | - if response.status_code != 200: |
133 | | - raise self._extract_error(response.content) from None |
| 128 | + if response.status_code < 200 and response.status_code >= 300: |
| 129 | + try: |
| 130 | + response_json = response.json() |
| 131 | + r_error = response_json.get("error",{}) |
| 132 | + error_message = response_json.get("detail", {}) or r_error.get("message", "Unknown Error") |
| 133 | + details = r_error.get("details", {}) |
| 134 | + error_code = r_error.get("code", "unknown_error") |
| 135 | + status_code = r_error.get("status_code", response.status_code) |
| 136 | + except JSONDecodeError: |
| 137 | + error_message = "Unknown error" |
| 138 | + details = {"raw": response.content.decode()} |
| 139 | + error_code ="unknown_error" |
| 140 | + status_code = response.status_code |
| 141 | + |
| 142 | + raise WorkflowAIError( |
| 143 | + error=BaseError( |
| 144 | + message=error_message, |
| 145 | + details=details, |
| 146 | + status_code=status_code, |
| 147 | + code=error_code, |
| 148 | + ), |
| 149 | + ) from None |
0 commit comments