diff --git a/src/claudesync/providers/claude_ai.py b/src/claudesync/providers/claude_ai.py index 5942042..d1d5586 100644 --- a/src/claudesync/providers/claude_ai.py +++ b/src/claudesync/providers/claude_ai.py @@ -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 @@ -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. " @@ -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):