Skip to content

Commit 32cdf4b

Browse files
committed
feat: enhance create_kb endpoint with error handling and JSON response
1 parent 4b1689c commit 32cdf4b

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

src/controllers/knowledge_base.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from fastapi import APIRouter, HTTPException
2+
from fastapi.responses import JSONResponse
23

34
from src.modules.create_kb.app.create_kb_controller import CreateKbController
45
from src.modules.get_presigned_bucket_url.app.get_presigned_bucket_url_controller import GetPresignedBucketUrlController
@@ -19,22 +20,32 @@ class CreateKbRequest(BaseModel):
1920

2021
@router.post("", summary="Cria uma base de conhecimento na AWS")
2122
async def create_kb(body: CreateKbRequest):
22-
use_case = CreateKbUseCase()
23-
controller = CreateKbController(use_case)
24-
body = {
25-
"kb_name": body.kb_name,
26-
"kb_description": body.kb_description
27-
}
28-
request = HttpRequest(body=body)
29-
response = controller(request)
23+
try:
24+
use_case = CreateKbUseCase()
25+
controller = CreateKbController(use_case)
26+
body_dict = {
27+
"kb_name": body.kb_name,
28+
"kb_description": body.kb_description
29+
}
30+
request = HttpRequest(body=body_dict)
31+
response = controller(request)
32+
33+
# Retornar resposta com o código HTTP correto
34+
return JSONResponse(
35+
status_code=response.status_code,
36+
content=response.body
37+
)
3038

31-
if not response:
39+
except Exception as e:
40+
# Fallback para erros não tratados pela controller interna
3241
raise HTTPException(
33-
status_code=500, detail="Algo deu errado ao criar a base de conhecimento"
42+
status_code=500,
43+
detail={
44+
"error": "Erro interno",
45+
"details": "Ocorreu um erro inesperado ao criar a base de conhecimento"
46+
}
3447
)
3548

36-
return response.data
37-
3849

3950
@router.get("/presigned-url", summary="Gera URL Presigned para enviar arquivos para uma base de conhecimento")
4051
async def get_url_presigned(bucket: str, user_id: str, kb_id: str, expires: int = 900, max_size_mb: int = 20):

0 commit comments

Comments
 (0)