🐛 fix(components.py): handle IntegrityError when creating a component to avoid duplicate entries
✨ feat(components.py): add error handling for creating a component with the same id to return a 400 status code and a detailed error message
This commit is contained in:
parent
6c1a6f3bd9
commit
587b203b66
1 changed files with 11 additions and 3 deletions
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue