From 587b203b669494744a3b13779d95ed6e803feaa7 Mon Sep 17 00:00:00 2001 From: gustavoschaedler Date: Wed, 12 Jul 2023 13:12:52 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(components.py):=20handle=20I?= =?UTF-8?q?ntegrityError=20when=20creating=20a=20component=20to=20avoid=20?= =?UTF-8?q?duplicate=20entries=20=E2=9C=A8=20feat(components.py):=20add=20?= =?UTF-8?q?error=20handling=20for=20creating=20a=20component=20with=20the?= =?UTF-8?q?=20same=20id=20to=20return=20a=20400=20status=20code=20and=20a?= =?UTF-8?q?=20detailed=20error=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/components.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/backend/langflow/api/v1/components.py b/src/backend/langflow/api/v1/components.py index 299f78371..f9cd8bce8 100644 --- a/src/backend/langflow/api/v1/components.py +++ b/src/backend/langflow/api/v1/components.py @@ -4,6 +4,7 @@ from langflow.database.models.component import Component from langflow.database.base import get_session from sqlmodel import Session, select from fastapi import APIRouter, Depends, HTTPException +from sqlalchemy.exc import IntegrityError COMPONENT_NOT_FOUND = "Component not found" @@ -13,9 +14,16 @@ router = APIRouter(prefix="/components", tags=["Components"]) @router.post("/", response_model=Component) def create_component(component: Component, db: Session = Depends(get_session)): - db.add(component) - db.commit() - db.refresh(component) + try: + db.add(component) + db.commit() + db.refresh(component) + except IntegrityError as e: + db.rollback() + raise HTTPException( + status_code=400, + detail="A component with the same id already exists.", + ) from e return component