Skip to content

Commit c17d2d8

Browse files
committed
Improve error handling
1 parent b1082bb commit c17d2d8

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ build/
2626
*.swo
2727

2828
# OS specific files
29-
.DS_Store
29+
.DS_Store
30+
31+
.git/

routes.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ async def check_httpx_err(body: str | bytes, api_key: str | None):
6969
body = body[6:]
7070
has_rate_limit_error, reset_time_ms = await check_rate_limit(body)
7171
if has_rate_limit_error:
72-
logger.warning("Rate limit detected in stream. Disabling key.")
7372
await key_manager.disable_key(api_key, reset_time_ms)
7473

7574

@@ -167,12 +166,11 @@ async def proxy_endpoint(
167166
return await proxy_with_httpx(
168167
request, path, api_key, is_stream, is_completion
169168
)
170-
171-
except (Exception, HTTPException) as e:
172-
logger.error("Error proxying request: %s", str(e))
173-
if isinstance(e, HTTPException):
174-
raise e
175-
raise HTTPException(status_code=500, detail=f"Proxy error: {str(e)}") from e
169+
except HTTPException as e:
170+
raise e
171+
except Exception as e:
172+
logger.error("Internal error: %s", str(e))
173+
raise HTTPException(status_code=500, detail="Internal Proxy Error") from e
176174

177175

178176
async def handle_completions(
@@ -228,7 +226,6 @@ async def stream_response() -> AsyncGenerator[bytes, None]:
228226
if api_key:
229227
has_rate_limit_error_, reset_time_ms_ = await check_rate_limit_chat(err)
230228
if has_rate_limit_error_:
231-
logger.warning("Rate limit detected in stream. Disabling key.")
232229
await key_manager.disable_key(
233230
api_key, reset_time_ms_
234231
)
@@ -256,27 +253,26 @@ async def stream_response() -> AsyncGenerator[bytes, None]:
256253
return Response(
257254
content=json.dumps(result), media_type="application/json"
258255
)
259-
except (APIError, Exception) as e:
256+
except APIError as e:
260257
logger.error("Error in chat completions: %s", str(e))
261-
code = 500
262-
detail = f"Error processing chat completion: {str(e)}"
263-
if isinstance(e, APIError):
264-
logger.debug("Error body: %s", e.body)
265-
# Check if this is a rate limit error
266-
if api_key:
267-
has_rate_limit_error, reset_time_ms = await check_rate_limit_chat(e)
268-
if has_rate_limit_error:
269-
logger.warning("Rate limit detected in stream. Disabling key.")
270-
await key_manager.disable_key(api_key, reset_time_ms)
271-
272-
# Try again with a new key
273-
new_api_key = await key_manager.get_next_key()
274-
if new_api_key:
275-
return await handle_completions(
276-
request, request_body, new_api_key, is_stream
277-
)
278-
code = e.code or code
279-
detail = e.body or detail
258+
logger.debug("Error body: %s", e.body)
259+
# Check if this is a rate limit error
260+
if api_key:
261+
has_rate_limit_error, reset_time_ms = await check_rate_limit_chat(e)
262+
if has_rate_limit_error:
263+
await key_manager.disable_key(api_key, reset_time_ms)
264+
265+
# Try again with a new key
266+
new_api_key = await key_manager.get_next_key()
267+
if new_api_key:
268+
return await handle_completions(
269+
request, request_body, new_api_key, is_stream
270+
)
271+
code = e.code or 500
272+
if e.code is None and isinstance(e.body, list) and isinstance(e.body[0], dict):
273+
code = e.body[0].get("error", {}).get("code", code)
274+
detail = e.body or f"Error processing chat completion: {str(e)}"
275+
280276
# Raise the exception
281277
raise HTTPException(code, detail) from e
282278

@@ -360,9 +356,9 @@ async def stream_completion():
360356
except httpx.TimeoutException as e:
361357
logger.error("Timeout connecting to OpenRouter: %s", str(e))
362358
raise HTTPException(504, "OpenRouter API request timed out") from e
363-
except Exception as e:
359+
except HTTPException as e:
364360
logger.error("Error proxying request with httpx: %s", str(e))
365-
raise HTTPException(500, f"Proxy error: {str(e)}") from e
361+
raise e
366362

367363

368364
@router.get("/health")

0 commit comments

Comments
 (0)