rely on user_manager and organization_manager for error throwing [LET-4637] (#5420)

rely on user_manager and organization_manager for error throwing

Co-authored-by: Ari Webb <ari@letta.com>
This commit is contained in:
Ari Webb
2025-10-14 11:35:23 -07:00
committed by Caren Thomas
parent e7ef73c0b6
commit 6904199cae
4 changed files with 17 additions and 50 deletions

View File

@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, List, Optional
from fastapi import APIRouter, Body, Depends, HTTPException, Query
from fastapi import APIRouter, Body, Depends, Query
from letta.schemas.organization import Organization, OrganizationCreate, OrganizationUpdate
from letta.server.rest_api.dependencies import get_letta_server
@@ -21,13 +21,7 @@ async def get_all_orgs(
"""
Get a list of all orgs in the database
"""
try:
orgs = await server.organization_manager.list_organizations_async(after=after, limit=limit)
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"{e}")
return orgs
return await server.organization_manager.list_organizations_async(after=after, limit=limit)
@router.post("/", tags=["admin"], response_model=Organization, operation_id="create_organization")
@@ -49,15 +43,9 @@ async def delete_org(
server: "SyncServer" = Depends(get_letta_server),
):
# TODO make a soft deletion, instead of a hard deletion
try:
org = await server.organization_manager.get_organization_by_id_async(org_id=org_id)
if org is None:
raise HTTPException(status_code=404, detail="Organization does not exist")
await server.organization_manager.delete_organization_by_id_async(org_id=org_id)
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"{e}")
# Get the org first so we can return it after deletion
org = await server.organization_manager.get_organization_by_id_async(org_id=org_id)
await server.organization_manager.delete_organization_by_id_async(org_id=org_id)
return org
@@ -67,13 +55,4 @@ async def update_org(
request: OrganizationUpdate = Body(...),
server: "SyncServer" = Depends(get_letta_server),
):
try:
org = await server.organization_manager.get_organization_by_id_async(org_id=org_id)
if org is None:
raise HTTPException(status_code=404, detail="Organization does not exist")
org = await server.organization_manager.update_organization_async(org_id=org_id, name=request.name)
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"{e}")
return org
return await server.organization_manager.update_organization_async(org_id=org_id, org_update=request)

View File

@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, List, Optional
from fastapi import APIRouter, Body, Depends, HTTPException, Query
from fastapi import APIRouter, Body, Depends, Query
from letta.schemas.user import User, UserCreate, UserUpdate
from letta.server.rest_api.dependencies import get_letta_server
@@ -22,13 +22,7 @@ async def list_users(
"""
Get a list of all users in the database
"""
try:
users = await server.user_manager.list_actors_async(after=after, limit=limit)
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"{e}")
return users
return await server.user_manager.list_actors_async(after=after, limit=limit)
@router.post("/", tags=["admin"], response_model=User, operation_id="create_user")
@@ -62,13 +56,7 @@ async def delete_user(
server: "SyncServer" = Depends(get_letta_server),
):
# TODO make a soft deletion, instead of a hard deletion
try:
user = await server.user_manager.get_actor_by_id_async(actor_id=user_id)
if user is None:
raise HTTPException(status_code=404, detail="User does not exist")
await server.user_manager.delete_actor_by_id_async(user_id=user_id)
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"{e}")
# Get the user first so we can return it after deletion
user = await server.user_manager.get_actor_by_id_async(actor_id=user_id)
await server.user_manager.delete_actor_by_id_async(user_id=user_id)
return user

View File

@@ -20,8 +20,8 @@ class OrganizationManager:
@enforce_types
@trace_method
async def get_organization_by_id_async(self, org_id: str) -> Optional[PydanticOrganization]:
"""Fetch an organization by ID."""
async def get_organization_by_id_async(self, org_id: str) -> PydanticOrganization:
"""Fetch an organization by ID. Raises NoResultFound if not found."""
async with db_registry.async_session() as session:
organization = await OrganizationModel.read_async(db_session=session, identifier=org_id)
return organization.to_pydantic()
@@ -64,7 +64,7 @@ class OrganizationManager:
@enforce_types
@trace_method
async def update_organization_async(self, org_id: str, org_update: OrganizationUpdate) -> PydanticOrganization:
"""Update an organization."""
"""Update an organization. Raises NoResultFound if not found."""
async with db_registry.async_session() as session:
org = await OrganizationModel.read_async(db_session=session, identifier=org_id)
if org_update.name:
@@ -77,7 +77,7 @@ class OrganizationManager:
@enforce_types
@trace_method
async def delete_organization_by_id_async(self, org_id: str):
"""Delete an organization by marking it as deleted."""
"""Delete an organization by marking it as deleted. Raises NoResultFound if not found."""
async with db_registry.async_session() as session:
organization = await OrganizationModel.read_async(db_session=session, identifier=org_id)
await organization.hard_delete_async(session)

View File

@@ -58,7 +58,7 @@ class UserManager:
@enforce_types
@trace_method
async def update_actor_async(self, user_update: UserUpdate) -> PydanticUser:
"""Update user details (async version)."""
"""Update user details (async version). Raises NoResultFound if not found."""
async with db_registry.async_session() as session:
# Retrieve the existing user by ID
existing_user = await UserModel.read_async(db_session=session, identifier=user_update.id)
@@ -76,7 +76,7 @@ class UserManager:
@enforce_types
@trace_method
async def delete_actor_by_id_async(self, user_id: str):
"""Delete a user and their associated records (agents, sources, mappings) asynchronously."""
"""Delete a user and their associated records (agents, sources, mappings) asynchronously. Raises NoResultFound if not found."""
async with db_registry.async_session() as session:
# Delete from user table
user = await UserModel.read_async(db_session=session, identifier=user_id)