Parses an event line and returns an error code if present, else None.
(
event_line: str | bytes, data_payload: SseDataPayload = _sse_data_payload
)
| 1092 | |
| 1093 | |
| 1094 | async def _parse_event_data_for_error( |
| 1095 | event_line: str | bytes, data_payload: SseDataPayload = _sse_data_payload |
| 1096 | ) -> int | None: |
| 1097 | """Parses an event line and returns an error code if present, else None.""" |
| 1098 | json_str: Final = data_payload(event_line) |
| 1099 | if json_str is not None: |
| 1100 | if not json_str or json_str == "[DONE]": # handle empty data or [DONE] message |
| 1101 | return None |
| 1102 | try: |
| 1103 | data: Final[object] = orjson.loads(json_str) |
| 1104 | if isinstance(data, dict) and "error" in data and isinstance(data["error"], dict): |
| 1105 | error_code_raw: Final = data["error"].get("code") |
| 1106 | error_code: int | None = None |
| 1107 | |
| 1108 | if isinstance(error_code_raw, int): |
| 1109 | error_code = error_code_raw |
| 1110 | elif isinstance(error_code_raw, str): |
| 1111 | try: |
| 1112 | error_code = int(error_code_raw) |
| 1113 | except ValueError: |
| 1114 | verbose_proxy_logger.warning( |
| 1115 | "Error code is a string but not a valid integer: %s", error_code_raw |
| 1116 | ) |
| 1117 | # Not a valid integer string, treat as if no valid code was found for this check |
| 1118 | |
| 1119 | # Ensure error_code is a valid HTTP status code |
| 1120 | if error_code is not None and 100 <= error_code <= 599: |
| 1121 | return error_code |
| 1122 | elif error_code_raw is not None: # Log if original code was present but not valid |
| 1123 | verbose_proxy_logger.warning("Error has invalid or non-convertible code: %s", error_code_raw) |
| 1124 | except (orjson.JSONDecodeError, json.JSONDecodeError): |
| 1125 | # not a known error chunk |
| 1126 | pass |
| 1127 | return None |
| 1128 | |
| 1129 | |
| 1130 | def _extract_error_from_sse_chunk(event_line: str | bytes, data_payload: SseDataPayload = _sse_data_payload) -> dict: |