Skip to content

Commit

Permalink
Error code based checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Cheong committed Mar 13, 2023
1 parent 87d8764 commit 92f0692
Showing 1 changed file with 21 additions and 44 deletions.
65 changes: 21 additions & 44 deletions src/revChatGPT/V1.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class ErrorType:
EXPIRED_ACCESS_TOKEN_ERROR = 4
INVALID_ACCESS_TOKEN_ERROR = 5
PROHIBITED_CONCURRENT_QUERY_ERROR = 6
AUTHENTICATION_ERROR = 7
CLOUDFLARE_ERROR = 8


class Error(Exception):
Expand Down Expand Up @@ -534,50 +536,25 @@ def ask(
continue
if not self.__check_fields(line) or response.status_code != 200:
log.error("Field missing", exc_info=True)
line_detail = line.get("detail")
if isinstance(line_detail, str):
if (
line_detail.lower()
== "too many requests in 1 hour. try again later."
):
log.error("Rate limit exceeded")
raise Error(
source="ask",
message=line.get("detail"),
code=ErrorType.RATE_LIMIT_ERROR,
)
if line_detail.lower().startswith(
"only one message at a time.",
):
log.error("Prohibited concurrent query")
raise Error(
source="ask",
message=line_detail,
code=ErrorType.PROHIBITED_CONCURRENT_QUERY_ERROR,
)
if line_detail.lower() == "invalid_api_key":
log.error("Invalid access token")
raise Error(
source="ask",
message=line_detail,
code=ErrorType.INVALID_REQUEST_ERROR,
)
if line_detail.lower() == "invalid_token":
log.error("Invalid access token")
raise Error(
source="ask",
message=line_detail,
code=ErrorType.INVALID_ACCESS_TOKEN_ERROR,
)
elif isinstance(line_detail, dict):
if line_detail.get("code") == "invalid_jwt":
log.error("Invalid access token")
raise Error(
source="ask",
message=line_detail.get("message", "invalid_jwt"),
code=ErrorType.INVALID_ACCESS_TOKEN_ERROR,
)

log.error(response.text)
if response.status_code == 401:
raise Error(
source="ask",
message="Permission denied",
code=ErrorType.AUTHENTICATION_ERROR,
)
if response.status_code == 403:
raise Error(
source="ask",
message="Cloudflare triggered a 403 error",
code=ErrorType.CLOUDFLARE_ERROR,
)
if response.status_code == 429:
raise Error(
source="ask",
message="Rate limit exceeded",
code=ErrorType.RATE_LIMIT_ERROR,
)
raise Error(
source="ask",
message=line,
Expand Down

0 comments on commit 92f0692

Please sign in to comment.