From 1a003fe4d3798ee2f1f9fd2a2bba6cf437862f6d Mon Sep 17 00:00:00 2001 From: Alessio <148966056+alessio-locatelli@users.noreply.github.com> Date: Thu, 19 Jun 2025 09:31:06 +0300 Subject: [PATCH] fix: wrap `_handle_request` to handle 5xx responses --- pr_agent/agent/pr_agent.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/pr_agent/agent/pr_agent.py b/pr_agent/agent/pr_agent.py index bfe924d6..bbe76f19 100644 --- a/pr_agent/agent/pr_agent.py +++ b/pr_agent/agent/pr_agent.py @@ -54,7 +54,7 @@ class PRAgent: def __init__(self, ai_handler: partial[BaseAiHandler,] = LiteLLMAIHandler): self.ai_handler = ai_handler # will be initialized in run_action - async def handle_request(self, pr_url, request, notify=None) -> bool: + async def _handle_request(self, pr_url, request, notify=None) -> bool: # First, apply repo specific settings if exists apply_repo_settings(pr_url) @@ -116,19 +116,25 @@ class PRAgent: if notify: notify() - try: - await command2class[action](pr_url, ai_handler=self.ai_handler, args=args).run() - except gitlab.GitlabError as gitlab_error: - # For GitLab 5xx codes see: https://docs.gitlab.com/api/rest/troubleshooting/#status-codes - if gitlab_error.response_code in {HTTP_500_INTERNAL_SERVER_ERROR, HTTP_503_SERVICE_UNAVAILABLE}: - # The problem is likely temporary and not on our side; therefore, do not fail the application. - get_logger().error( - f"Failed to process the command due to a problem on the GitLab API side: {gitlab_error!r}\n" - + "Check https://status.gitlab.com and try again later." - ) - return False - raise - + await command2class[action](pr_url, ai_handler=self.ai_handler, args=args).run() else: return False return True + + async def handle_request(self, pr_url, request, notify=None) -> bool: + # We wrap the entire entry point to gracefully handle third-party + # service errors (such as the GitLab API) to cover cases when such + # errors were not handled or retried by the low-level code or upstream + # libraries. However, we avoid a bare `except` here to not silence + # critical errors on our side. + try: + return await self._handle_request(pr_url, request, notify) + except gitlab.GitlabError as gitlab_error: + # GitLab 5xx codes: https://docs.gitlab.com/api/rest/troubleshooting/#status-codes + if gitlab_error.response_code in {HTTP_500_INTERNAL_SERVER_ERROR, HTTP_503_SERVICE_UNAVAILABLE}: + get_logger().error( + f"Failed to process the command due to a problem on the GitLab API side: {gitlab_error!r}\n" + + "Check https://status.gitlab.com and try again later." + ) + return False + raise