Skip to content

Commit

Permalink
Merge pull request #10 from led-mirage/feature/improve-error-messages
Browse files Browse the repository at this point in the history
Anthropic APIでAPIエラーが発生したときのメッセージを改善
  • Loading branch information
led-mirage authored Jan 2, 2025
2 parents 25be352 + 52cd265 commit c7f9b27
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
22 changes: 13 additions & 9 deletions app/chat/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# チャットクラス
#
# Copyright (c) 2024 led-mirage
# Copyright (c) 2024-2025 led-mirage
# このソースコードは MITライセンス の下でライセンスされています。
# ライセンスの詳細については、このプロジェクトのLICENSEファイルを参照してください。

Expand Down Expand Up @@ -51,7 +51,7 @@ def send_message(
recieve_chunk: Callable[[str], None],
recieve_sentence: Callable[[str], None],
end_response: Callable[[str], None],
on_error: Callable[[Exception, str], None]) -> str:
on_error: Callable[[Exception, str, str | None], None]) -> str:

try:
self.stop_send_event.clear()
Expand Down Expand Up @@ -349,19 +349,23 @@ def send_message(
on_error(e, "RateLimit")
except anthropic.APIStatusError as e:
if e.status_code == 400:
on_error(e, "BadRequest")
on_error(e, "APIError", "Invalid Request(400)")
elif e.status_code == 401:
on_error(e, "Authentication")
elif e.status_code == 403:
on_error(e, "PermissionDeniedError")
on_error(e, "APIError", "Permission Denied(403)")
elif e.status_code == 404:
on_error(e, "APIError", "Not Found(404)")
elif e.status_code == 413:
on_error(e, "APIError", "Request too large(413)")
elif e.status_code == 422:
on_error(e, "UnprocessableEntity")
on_error(e, "APIError", "UnprocessableEntity(422)")
elif e.status_code == 429:
on_error(e, "RateLimit")
elif e.status_code == 500:
on_error(e, "InternalServerError")
on_error(e, "APIError", "RateLimit")
elif e.status_code == 529:
on_error(e, "APIError", "Overloaded(529)")
else:
on_error(e, "APIConnectionError")
on_error(e, "APIError", f"Internal Server Error({e.status_code})")
except Exception as e:
on_error(e, "Exception")

Expand Down
6 changes: 4 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import pyi_splash # type: ignore

APP_NAME = "ZundaGPT2 Lite"
APP_VERSION = "1.4.2"
APP_VERSION = "1.4.3-dev"
COPYRIGHT = "Copyright 2024-2025 led-mirage"

# アプリケーションクラス
Expand Down Expand Up @@ -242,7 +242,7 @@ def on_end_response(self, content):
self._window.evaluate_js(f"endResponse('{self.escape_js_string(content)}')")

# チャット例外イベントハンドラ(Chat)
def on_chat_error(self, e: Exception, cause: str):
def on_chat_error(self, e: Exception, cause: str, info: str=""):
module_name = type(e).__module__
class_name = type(e).__name__
print(f"{module_name}.{class_name}")
Expand All @@ -260,6 +260,8 @@ def on_chat_error(self, e: Exception, cause: str):
message = "会話の内容が不適切だと判断されたのだ"
elif cause == "RateLimit":
message = "レート制限に達したのだ"
elif cause == "APIError":
message = f"APIエラーが発生したのだ\n{info}"
else:
message = f"なんかわからないエラーが発生したのだ({class_name})"
self._window.evaluate_js(f"handleChatException('{message}')")
Expand Down

0 comments on commit c7f9b27

Please sign in to comment.