@@ -81,7 +81,7 @@ async def upload_file(
8181 try :
8282 result = await response .json ()
8383 except json .JSONDecodeError :
84- raise Exception (f"文件上传响应解析失败: { response_text } " )
84+ raise Exception (f"文件上传响应解析失败: { response_text } " ) from None
8585
8686 if result .get ("code" ) != 0 :
8787 raise Exception (f"文件上传失败: { result .get ('msg' , '未知错误' )} " )
@@ -90,12 +90,12 @@ async def upload_file(
9090 logger .debug (f"[Coze] 图片上传成功,file_id: { file_id } " )
9191 return file_id
9292
93- except asyncio . TimeoutError :
93+ except TimeoutError :
9494 logger .error ("文件上传超时" )
95- raise Exception ("文件上传超时" )
95+ raise Exception ("文件上传超时" ) from None
9696 except Exception as e :
9797 logger .error (f"文件上传失败: { e !s} " )
98- raise Exception (f"文件上传失败: { e !s} " )
98+ raise Exception (f"文件上传失败: { e !s} " ) from e
9999
100100 async def download_image (self , image_url : str ) -> bytes :
101101 """下载图片并返回字节数据
@@ -118,7 +118,7 @@ async def download_image(self, image_url: str) -> bytes:
118118
119119 except Exception as e :
120120 logger .error (f"下载图片失败 { image_url } : { e !s} " )
121- raise Exception (f"下载图片失败: { e !s} " )
121+ raise Exception (f"下载图片失败: { e !s} " ) from e
122122
123123 async def chat_messages (
124124 self ,
@@ -203,10 +203,10 @@ async def chat_messages(
203203 except json .JSONDecodeError :
204204 event_data = {"content" : data_str }
205205
206- except asyncio . TimeoutError :
207- raise Exception (f"Coze API 流式请求超时 ({ timeout } 秒)" )
206+ except TimeoutError :
207+ raise Exception (f"Coze API 流式请求超时 ({ timeout } 秒)" ) from None
208208 except Exception as e :
209- raise Exception (f"Coze API 流式请求失败: { e !s} " )
209+ raise Exception (f"Coze API 流式请求失败: { e !s} " ) from e
210210
211211 async def clear_context (self , conversation_id : str ):
212212 """清空会话上下文
@@ -234,12 +234,12 @@ async def clear_context(self, conversation_id: str):
234234 try :
235235 return json .loads (response_text )
236236 except json .JSONDecodeError :
237- raise Exception ("Coze API 返回非JSON格式" )
237+ raise Exception ("Coze API 返回非JSON格式" ) from None
238238
239- except asyncio . TimeoutError :
240- raise Exception ("Coze API 请求超时" )
239+ except TimeoutError :
240+ raise Exception ("Coze API 请求超时" ) from None
241241 except aiohttp .ClientError as e :
242- raise Exception (f"Coze API 请求失败: { e !s} " )
242+ raise Exception (f"Coze API 请求失败: { e !s} " ) from e
243243
244244 async def get_message_list (
245245 self ,
@@ -275,7 +275,7 @@ async def get_message_list(
275275
276276 except Exception as e :
277277 logger .error (f"获取Coze消息列表失败: { e !s} " )
278- raise Exception (f"获取Coze消息列表失败: { e !s} " )
278+ raise Exception (f"获取Coze消息列表失败: { e !s} " ) from e
279279
280280 async def close (self ) -> None :
281281 """关闭会话"""
@@ -299,7 +299,7 @@ async def test_coze_api_client() -> None:
299299 async with await anyio .open_file ("README.md" , "rb" ) as f :
300300 file_data = await f .read ()
301301 file_id = await client .upload_file (file_data )
302- async for event in client .chat_messages (
302+ async for _event in client .chat_messages (
303303 bot_id = bot_id ,
304304 user_id = "test_user" ,
305305 additional_messages = [
0 commit comments