Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

refactor(backend): Improve error message on unmatched webhook ingress (#9694)

Reinier van der Leer87f87500cba3
diff --git a/autogpt_platform/backend/backend/data/integrations.py b/autogpt_platform/backend/backend/data/integrations.py
index 2ed06e2015..29e0ff7efb 100644
--- a/autogpt_platform/backend/backend/data/integrations.py
+++ b/autogpt_platform/backend/backend/data/integrations.py
@@ -9,6 +9,7 @@ from backend.data.includes import INTEGRATION_WEBHOOK_INCLUDE
from backend.data.queue import AsyncRedisEventBus
from backend.integrations.providers import ProviderName
from backend.integrations.webhooks.utils import webhook_ingress_url
+from backend.util.exceptions import NotFoundError
from .db import BaseDbModel
@@ -82,11 +83,18 @@ async def create_webhook(webhook: Webhook) -> Webhook:
async def get_webhook(webhook_id: str) -> Webhook:
- """⚠️ No `user_id` check: DO NOT USE without check in user-facing endpoints."""
- webhook = await IntegrationWebhook.prisma().find_unique_or_raise(
+ """
+ ⚠️ No `user_id` check: DO NOT USE without check in user-facing endpoints.
+
+ Raises:
+ NotFoundError: if no record with the given ID exists
+ """
+ webhook = await IntegrationWebhook.prisma().find_unique(
where={"id": webhook_id},
include=INTEGRATION_WEBHOOK_INCLUDE,
)
+ if not webhook:
+ raise NotFoundError(f"Webhook #{webhook_id} not found")
return Webhook.from_db(webhook)
diff --git a/autogpt_platform/backend/backend/server/integrations/router.py b/autogpt_platform/backend/backend/server/integrations/router.py
index 1cae379de0..00e3cedd7d 100644
--- a/autogpt_platform/backend/backend/server/integrations/router.py
+++ b/autogpt_platform/backend/backend/server/integrations/router.py
@@ -3,6 +3,7 @@ from typing import TYPE_CHECKING, Annotated, Literal
from fastapi import APIRouter, Body, Depends, HTTPException, Path, Query, Request
from pydantic import BaseModel, Field
+from starlette.status import HTTP_404_NOT_FOUND
from backend.data.graph import set_node_webhook
from backend.data.integrations import (
@@ -18,7 +19,7 @@ from backend.integrations.creds_manager import IntegrationCredentialsManager
from backend.integrations.oauth import HANDLERS_BY_NAME
from backend.integrations.providers import ProviderName
from backend.integrations.webhooks import get_webhook_manager
-from backend.util.exceptions import NeedConfirmation
+from backend.util.exceptions import NeedConfirmation, NotFoundError
from backend.util.service import get_service_client
from backend.util.settings import Settings
@@ -282,7 +283,13 @@ async def webhook_ingress_generic(
):
logger.debug(f"Received {provider.value} webhook ingress for ID {webhook_id}")
webhook_manager = get_webhook_manager(provider)
- webhook = await get_webhook(webhook_id)
+ try:
+ webhook = await get_webhook(webhook_id)
+ except NotFoundError as e:
+ logger.warning(f"Webhook payload received for unknown webhook: {e}")
+ raise HTTPException(
+ status_code=HTTP_404_NOT_FOUND, detail=f"Webhook #{webhook_id} not found"
+ ) from e
logger.debug(f"Webhook #{webhook_id}: {webhook}")
payload, event_type = await webhook_manager.validate_payload(webhook, request)
logger.debug(
diff --git a/autogpt_platform/backend/backend/util/exceptions.py b/autogpt_platform/backend/backend/util/exceptions.py
index eb6e42bda7..0d0fa0cf96 100644
--- a/autogpt_platform/backend/backend/util/exceptions.py
+++ b/autogpt_platform/backend/backend/util/exceptions.py
@@ -2,6 +2,10 @@ class MissingConfigError(Exception):
"""The attempted operation requires configuration which is not available"""
+class NotFoundError(ValueError):
+ """The requested record was not found, resulting in an error condition"""
+
+
class NeedConfirmation(Exception):
"""The user must explicitly confirm that they want to proceed"""