Skip to content

Commit

Permalink
Propagate clean error message from github error to user
Browse files Browse the repository at this point in the history
  • Loading branch information
majdyz committed Oct 26, 2024
1 parent 0343402 commit 90d1fc7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
24 changes: 21 additions & 3 deletions autogpt_platform/backend/backend/integrations/webhooks/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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
8 changes: 6 additions & 2 deletions autogpt_platform/backend/backend/server/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 90d1fc7

Please sign in to comment.