Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve a google rate limit error handling: google_search encountered an KeyError: 'x-ratelimit-limit' in handle_exceptions() 3 #471

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion services/google/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def scrape_content_from_url(url: str):

@handle_exceptions(default_return_value=[], raise_on_error=False, api_type="google")
def google_search(
base_args: BaseArgs,
base_args: BaseArgs, # pylint: disable=unused-argument
query: str,
num_results: int = NUM_RESULTS_DEFAULT,
lang: str = "en",
Expand Down
3 changes: 3 additions & 0 deletions utils/handle_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def wrapper(*args: Tuple[Any, ...], **kwargs: Any):
except requests.exceptions.HTTPError as err:
reason: str | Any = err.response.reason
text: str | Any = err.response.text
status_code: int = err.response.status_code
print(f"reason: {reason}, text: {text}, status_code: {status_code}")

if api_type == "github" and err.response.status_code in {403, 429}:
limit = int(err.response.headers["X-RateLimit-Limit"])
Expand Down Expand Up @@ -65,6 +67,7 @@ def wrapper(*args: Tuple[Any, ...], **kwargs: Any):

elif api_type == "google" and err.response.status_code == 429:
retry_after = int(err.response.headers.get("Retry-After", 60))
print(f"retry_after: {retry_after}")
err_msg = f"Google Search Rate Limit: {func.__name__} will retry after {retry_after} seconds"
logging.warning(msg=err_msg)
time.sleep(retry_after)
Expand Down
Loading