Skip to content

Commit

Permalink
Handle message limits
Browse files Browse the repository at this point in the history
  • Loading branch information
jahwag committed Aug 21, 2024
1 parent c770597 commit f841ece
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/claudesync/providers/claude_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import urllib.parse
import json
import gzip
from datetime import datetime, timezone

from .base_claude_ai import BaseClaudeAIProvider
from ..exceptions import ProviderError

Expand Down Expand Up @@ -71,11 +73,11 @@ def _make_request(self, method, endpoint, data=None):
raise ProviderError(f"Invalid JSON response from API: {str(json_err)}")

def handle_http_error(self, e):
self.logger.error(f"Request failed: {str(e)}")
self.logger.error(f"Response status code: {e.code}")
self.logger.error(f"Response headers: {e.headers}")
self.logger.debug(f"Request failed: {str(e)}")
self.logger.debug(f"Response status code: {e.code}")
self.logger.debug(f"Response headers: {e.headers}")
content = e.read().decode("utf-8")
self.logger.error(f"Response content: {content}")
self.logger.debug(f"Response content: {content}")
if e.code == 403:
error_msg = (
"Received a 403 Forbidden error. Your session key might be invalid. "
Expand All @@ -86,6 +88,18 @@ def handle_http_error(self, e):
)
self.logger.error(error_msg)
raise ProviderError(error_msg)
if e.code == 429:
try:
error_data = json.loads(content)
resets_at_unix = json.loads(error_data["error"]["message"])["resetsAt"]
resets_at_local = datetime.fromtimestamp(
resets_at_unix, tz=timezone.utc
).astimezone()
formatted_time = resets_at_local.strftime("%a %b %d %Y %H:%M:%S %Z%z")
print(f"Message limit exceeded. Try again after {formatted_time}")
except (KeyError, json.JSONDecodeError) as parse_error:
print(f"Failed to parse error response: {parse_error}")
raise ProviderError(f"HTTP 429: Too Many Requests")
raise ProviderError(f"API request failed: {str(e)}")

def _make_request_stream(self, method, endpoint, data=None):
Expand Down

0 comments on commit f841ece

Please sign in to comment.