11import logging
22from http import HTTPStatus
3- from http .client import HTTPException
43from typing import Optional , Dict
54import uuid
65
7- from fastapi import APIRouter , Body , Header , Request
6+ from fastapi import APIRouter , Body , Header , Request , HTTPException
87from fastapi .responses import JSONResponse
98
109from consts .exceptions import UnauthorizedError , LimitExceededError , SignatureValidationError
@@ -57,7 +56,8 @@ async def _parse_northbound_context(request: Request) -> NorthboundContext:
5756 validate_aksk_authentication (request .headers , request_body )
5857 except (UnauthorizedError , LimitExceededError , SignatureValidationError ) as e :
5958 raise e
60- except Exception :
59+ except Exception as e :
60+ logging .error (f"Failed to parse northbound context: { str (e )} " , exc_info = e )
6161 raise HTTPException (status_code = HTTPStatus .INTERNAL_SERVER_ERROR ,
6262 detail = "Internal Server Error: cannot parse northbound context" )
6363
@@ -78,7 +78,11 @@ async def _parse_northbound_context(request: Request) -> NorthboundContext:
7878 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
7979 detail = "Unauthorized: unregistered user_id in JWT token" )
8080
81- except Exception :
81+ except HTTPException as e :
82+ # Preserve explicit HTTP errors raised during JWT parsing
83+ raise e
84+ except Exception as e :
85+ logging .error (f"Failed to parse JWT token: { str (e )} " , exc_info = e )
8286 raise HTTPException (status_code = HTTPStatus .INTERNAL_SERVER_ERROR ,
8387 detail = "Internal Server Error: cannot parse JWT token" )
8488
@@ -115,16 +119,23 @@ async def run_chat(
115119 query = query ,
116120 idempotency_key = idempotency_key ,
117121 )
118- except UnauthorizedError :
122+ except UnauthorizedError as e :
123+ logging .error (f"Unauthorized: AK/SK authentication failed: { str (e )} " , exc_info = e )
119124 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
120125 detail = "Unauthorized: AK/SK authentication failed" )
121- except LimitExceededError :
126+ except LimitExceededError as e :
127+ logging .error (f"Too Many Requests: rate limit exceeded: { str (e )} " , exc_info = e )
122128 raise HTTPException (status_code = HTTPStatus .TOO_MANY_REQUESTS ,
123129 detail = "Too Many Requests: rate limit exceeded" )
124- except SignatureValidationError :
130+ except SignatureValidationError as e :
131+ logging .error (f"Unauthorized: invalid signature: { str (e )} " , exc_info = e )
125132 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
126133 detail = "Unauthorized: invalid signature" )
127- except Exception :
134+ except HTTPException as e :
135+ # Propagate HTTP errors from context parsing without altering status/detail
136+ raise e
137+ except Exception as e :
138+ logging .error (f"Failed to run chat: { str (e )} " , exc_info = e )
128139 raise HTTPException (
129140 status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = "Internal Server Error" )
130141
@@ -134,16 +145,22 @@ async def stop_chat_stream(request: Request, conversation_id: str):
134145 try :
135146 ctx : NorthboundContext = await _parse_northbound_context (request )
136147 return await stop_chat (ctx = ctx , external_conversation_id = conversation_id )
137- except UnauthorizedError :
148+ except UnauthorizedError as e :
149+ logging .error (f"Unauthorized: AK/SK authentication failed: { str (e )} " , exc_info = e )
138150 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
139151 detail = "Unauthorized: AK/SK authentication failed" )
140- except LimitExceededError :
152+ except LimitExceededError as e :
153+ logging .error (f"Too Many Requests: rate limit exceeded: { str (e )} " , exc_info = e )
141154 raise HTTPException (status_code = HTTPStatus .TOO_MANY_REQUESTS ,
142155 detail = "Too Many Requests: rate limit exceeded" )
143- except SignatureValidationError :
156+ except SignatureValidationError as e :
157+ logging .error (f"Unauthorized: invalid signature: { str (e )} " , exc_info = e )
144158 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
145159 detail = "Unauthorized: invalid signature" )
146- except Exception :
160+ except HTTPException as e :
161+ raise e
162+ except Exception as e :
163+ logging .error (f"Failed to stop chat: { str (e )} " , exc_info = e )
147164 raise HTTPException (
148165 status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = "Internal Server Error" )
149166
@@ -153,16 +170,22 @@ async def get_history(request: Request, conversation_id: str):
153170 try :
154171 ctx : NorthboundContext = await _parse_northbound_context (request )
155172 return await get_conversation_history (ctx = ctx , external_conversation_id = conversation_id )
156- except UnauthorizedError :
173+ except UnauthorizedError as e :
174+ logging .error (f"Unauthorized: AK/SK authentication failed: { str (e )} " , exc_info = e )
157175 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
158176 detail = "Unauthorized: AK/SK authentication failed" )
159- except LimitExceededError :
177+ except LimitExceededError as e :
178+ logging .error (f"Too Many Requests: rate limit exceeded: { str (e )} " , exc_info = e )
160179 raise HTTPException (status_code = HTTPStatus .TOO_MANY_REQUESTS ,
161180 detail = "Too Many Requests: rate limit exceeded" )
162- except SignatureValidationError :
181+ except SignatureValidationError as e :
182+ logging .error (f"Unauthorized: invalid signature: { str (e )} " , exc_info = e )
163183 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
164184 detail = "Unauthorized: invalid signature" )
165- except Exception :
185+ except HTTPException as e :
186+ raise e
187+ except Exception as e :
188+ logging .error (f"Failed to get conversation history: { str (e )} " , exc_info = e )
166189 raise HTTPException (
167190 status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = "Internal Server Error" )
168191
@@ -172,16 +195,22 @@ async def list_agents(request: Request):
172195 try :
173196 ctx : NorthboundContext = await _parse_northbound_context (request )
174197 return await get_agent_info_list (ctx = ctx )
175- except UnauthorizedError :
198+ except UnauthorizedError as e :
199+ logging .error (f"Unauthorized: AK/SK authentication failed: { str (e )} " , exc_info = e )
176200 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
177201 detail = "Unauthorized: AK/SK authentication failed" )
178- except LimitExceededError :
202+ except LimitExceededError as e :
203+ logging .error (f"Too Many Requests: rate limit exceeded: { str (e )} " , exc_info = e )
179204 raise HTTPException (status_code = HTTPStatus .TOO_MANY_REQUESTS ,
180205 detail = "Too Many Requests: rate limit exceeded" )
181- except SignatureValidationError :
206+ except SignatureValidationError as e :
207+ logging .error (f"Unauthorized: invalid signature: { str (e )} " , exc_info = e )
182208 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
183209 detail = "Unauthorized: invalid signature" )
184- except Exception :
210+ except HTTPException as e :
211+ raise e
212+ except Exception as e :
213+ logging .error (f"Failed to list agents: { str (e )} " , exc_info = e )
185214 raise HTTPException (
186215 status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = "Internal Server Error" )
187216
@@ -191,16 +220,22 @@ async def list_convs(request: Request):
191220 try :
192221 ctx : NorthboundContext = await _parse_northbound_context (request )
193222 return await list_conversations (ctx = ctx )
194- except UnauthorizedError :
223+ except UnauthorizedError as e :
224+ logging .error (f"Unauthorized: AK/SK authentication failed: { str (e )} " , exc_info = e )
195225 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
196226 detail = "Unauthorized: AK/SK authentication failed" )
197- except LimitExceededError :
227+ except LimitExceededError as e :
228+ logging .error (f"Too Many Requests: rate limit exceeded: { str (e )} " , exc_info = e )
198229 raise HTTPException (status_code = HTTPStatus .TOO_MANY_REQUESTS ,
199230 detail = "Too Many Requests: rate limit exceeded" )
200- except SignatureValidationError :
231+ except SignatureValidationError as e :
232+ logging .error (f"Unauthorized: invalid signature: { str (e )} " , exc_info = e )
201233 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
202234 detail = "Unauthorized: invalid signature" )
203- except Exception :
235+ except HTTPException as e :
236+ raise e
237+ except Exception as e :
238+ logging .error (f"Failed to list conversations: { str (e )} " , exc_info = e )
204239 raise HTTPException (
205240 status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = "Internal Server Error" )
206241
@@ -224,15 +259,21 @@ async def update_convs_title(
224259 "Idempotency-Key" : result .get ("idempotency_key" , "" ), "X-Request-Id" : ctx .request_id }
225260 return JSONResponse (content = result , headers = headers_out )
226261
227- except UnauthorizedError :
262+ except UnauthorizedError as e :
263+ logging .error (f"Unauthorized: AK/SK authentication failed: { str (e )} " , exc_info = e )
228264 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
229265 detail = "Unauthorized: AK/SK authentication failed" )
230- except LimitExceededError :
266+ except LimitExceededError as e :
267+ logging .error (f"Too Many Requests: rate limit exceeded: { str (e )} " , exc_info = e )
231268 raise HTTPException (status_code = HTTPStatus .TOO_MANY_REQUESTS ,
232269 detail = "Too Many Requests: rate limit exceeded" )
233- except SignatureValidationError :
270+ except SignatureValidationError as e :
271+ logging .error (f"Unauthorized: invalid signature: { str (e )} " , exc_info = e )
234272 raise HTTPException (status_code = HTTPStatus .UNAUTHORIZED ,
235273 detail = "Unauthorized: invalid signature" )
236- except Exception :
274+ except HTTPException as e :
275+ raise e
276+ except Exception as e :
277+ logging .error (f"Failed to update conversation title: { str (e )} " , exc_info = e )
237278 raise HTTPException (
238279 status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = "Internal Server Error" )
0 commit comments