diff --git a/autogpt_platform/backend/backend/integrations/webhooks/github.py b/autogpt_platform/backend/backend/integrations/webhooks/github.py index c6714a9b4b12..0c69368e452d 100644 --- a/autogpt_platform/backend/backend/integrations/webhooks/github.py +++ b/autogpt_platform/backend/backend/integrations/webhooks/github.py @@ -19,6 +19,21 @@ class GithubWebhookType(StrEnum): REPO = "repo" +def extract_github_error_msg(response: requests.Response) -> str: + error_msgs = [] + resp = response.json() + if resp.get("message"): + error_msgs.append(resp["message"]) + if resp.get("errors"): + error_msgs.extend(f"* {err.get('message', err)}" for err in resp["errors"]) + if resp.get("error"): + if isinstance(resp["error"], dict): + error_msgs.append(resp["error"].get("message", resp["error"])) + else: + error_msgs.append(resp["error"]) + return "\n".join(error_msgs) + + class GithubWebhooksManager(BaseWebhooksManager): PROVIDER_NAME = ProviderName.GITHUB @@ -70,7 +85,8 @@ async def trigger_ping(self, webhook: integrations.Webhook) -> None: response = requests.post(ping_url, headers=headers) if response.status_code != 204: - raise ValueError(f"Failed to ping GitHub webhook: {response.text}") + error_msg = extract_github_error_msg(response) + raise ValueError(f"Failed to ping GitHub webhook: {error_msg}") async def _register_webhook( self, @@ -110,7 +126,8 @@ async def _register_webhook( ) if response.status_code != 201: - raise ValueError(f"Failed to create GitHub webhook: {response.text}") + error_msg = extract_github_error_msg(response) + raise ValueError(f"Failed to create GitHub webhook: {error_msg}") webhook_id = response.json()["id"] config = response.json()["config"] @@ -143,6 +160,7 @@ async def _deregister_webhook( if response.status_code not in [204, 404]: # 204 means successful deletion, 404 means the webhook was already deleted - raise ValueError(f"Failed to delete GitHub webhook: {response.text}") + error_msg = extract_github_error_msg(response) + raise ValueError(f"Failed to delete GitHub webhook: {error_msg}") # If we reach here, the webhook was successfully deleted or didn't exist diff --git a/autogpt_platform/backend/backend/server/rest_api.py b/autogpt_platform/backend/backend/server/rest_api.py index 700ef62cca39..80c118f6b481 100644 --- a/autogpt_platform/backend/backend/server/rest_api.py +++ b/autogpt_platform/backend/backend/server/rest_api.py @@ -268,7 +268,9 @@ def run_service(self): tags=["settings"], ) - app.add_exception_handler(ValueError, self.handle_internal_http_error(400)) + app.add_exception_handler( + ValueError, self.handle_internal_http_error(400, False) + ) app.add_exception_handler(500, self.handle_internal_http_error(500)) app.include_router(api_router) app.include_router(health_router) @@ -326,8 +328,10 @@ def execution_scheduler_client(self) -> ExecutionScheduler: return get_service_client(ExecutionScheduler) @classmethod - def handle_internal_http_error(cls, status_code: int = 500): + def handle_internal_http_error(cls, status_code: int = 500, log_error: bool = True): def handler(request: Request, exc: Exception): + if log_error: + logger.exception(f"{request.method} {request.url.path} failed: {exc}") return JSONResponse( content={ "message": f"{request.method} {request.url.path} failed",