improve error handle #3

Merged
hibobmaster merged 1 commit from dev into main 2023-04-13 05:44:23 +00:00
4 changed files with 21 additions and 13 deletions

View file

@ -36,4 +36,4 @@ class askGPT:
resp = await response.read() resp = await response.read()
return json.loads(resp)['choices'][0]['message']['content'] return json.loads(resp)['choices'][0]['message']['content']
except Exception as e: except Exception as e:
logger.error("Error Exception", exc_info=True) raise Exception(e)

View file

@ -47,6 +47,6 @@ class BingBot:
return json_body['response'] return json_body['response']
except Exception as e: except Exception as e:
logger.error("Error Exception", exc_info=True) logger.error("Error Exception", exc_info=True)
print(f"Error: {e}")
pass
return "Error, please retry" return "Error, please retry"

27
bot.py
View file

@ -495,14 +495,14 @@ class Bot:
try: try:
text = await asyncio.wait_for(self.chatbot.ask_async(prompt), timeout=180) text = await asyncio.wait_for(self.chatbot.ask_async(prompt), timeout=180)
except TimeoutError as e: except TimeoutError as e:
logger.error("timeoutException", exc_info=True) logger.error(f"TimeoutException: {e}", exc_info=True)
text = "Timeout error" raise Exception("Timeout error")
except Exception as e: except Exception as e:
logger.error("Error", exc_info=True) raise Exception(e)
print(f"Error: {e}")
text = text.strip()
try: try:
text = text.strip()
await send_room_message(self.client, room_id, reply_message=text, await send_room_message(self.client, room_id, reply_message=text,
reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted) reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted)
except Exception as e: except Exception as e:
@ -516,11 +516,14 @@ class Bot:
# timeout 120s # timeout 120s
text = await asyncio.wait_for(self.askgpt.oneTimeAsk(prompt, self.chatgpt_api_endpoint, self.headers), timeout=180) text = await asyncio.wait_for(self.askgpt.oneTimeAsk(prompt, self.chatgpt_api_endpoint, self.headers), timeout=180)
except TimeoutError: except TimeoutError:
logger.error("timeoutException", exc_info=True) logger.error("TimeoutException", exc_info=True)
text = "Timeout error" raise Exception("Timeout error")
except Exception as e:
raise Exception(e)
text = text.strip()
try: try:
text = text.strip()
await send_room_message(self.client, room_id, reply_message=text, await send_room_message(self.client, room_id, reply_message=text,
reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted) reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted)
except Exception as e: except Exception as e:
@ -535,9 +538,13 @@ class Bot:
text = await asyncio.wait_for(self.bingbot.ask_bing(prompt), timeout=180) text = await asyncio.wait_for(self.bingbot.ask_bing(prompt), timeout=180)
except TimeoutError: except TimeoutError:
logger.error("timeoutException", exc_info=True) logger.error("timeoutException", exc_info=True)
text = "Timeout error" raise Exception("Timeout error")
text = text.strip() except Exception as e:
raise Exception(e)
try: try:
text = text.strip()
await send_room_message(self.client, room_id, reply_message=text, await send_room_message(self.client, room_id, reply_message=text,
reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted) reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted)
except Exception as e: except Exception as e:

View file

@ -59,3 +59,4 @@ async def send_room_image(client: AsyncClient,
except Exception as e: except Exception as e:
logger.error( logger.error(
f"Image send of file {image} failed.\n Error: {e}", exc_info=True) f"Image send of file {image} failed.\n Error: {e}", exc_info=True)
raise Exception(e)