refac: error handling

This commit is contained in:
Timothy J. Baek 2024-09-04 17:52:59 +02:00
parent e5cfa6501b
commit 8f6369374d

View File

@ -739,10 +739,16 @@ class PipelineMiddleware(BaseHTTPMiddleware):
try: try:
data = filter_pipeline(data, user) data = filter_pipeline(data, user)
except Exception as e: except Exception as e:
return JSONResponse( if len(e.args) > 1:
status_code=e.args[0], return JSONResponse(
content={"detail": e.args[1]}, status_code=e.args[0],
) content={"detail": e.args[1]},
)
else:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"detail": str(e)},
)
modified_body_bytes = json.dumps(data).encode("utf-8") modified_body_bytes = json.dumps(data).encode("utf-8")
# Replace the request body with the modified one # Replace the request body with the modified one
@ -1390,10 +1396,16 @@ async def generate_title(form_data: dict, user=Depends(get_verified_user)):
try: try:
payload = filter_pipeline(payload, user) payload = filter_pipeline(payload, user)
except Exception as e: except Exception as e:
return JSONResponse( if len(e.args) > 1:
status_code=e.args[0], return JSONResponse(
content={"detail": e.args[1]}, status_code=e.args[0],
) content={"detail": e.args[1]},
)
else:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"detail": str(e)},
)
if "chat_id" in payload: if "chat_id" in payload:
del payload["chat_id"] del payload["chat_id"]
@ -1443,10 +1455,16 @@ async def generate_search_query(form_data: dict, user=Depends(get_verified_user)
try: try:
payload = filter_pipeline(payload, user) payload = filter_pipeline(payload, user)
except Exception as e: except Exception as e:
return JSONResponse( if len(e.args) > 1:
status_code=e.args[0], return JSONResponse(
content={"detail": e.args[1]}, status_code=e.args[0],
) content={"detail": e.args[1]},
)
else:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"detail": str(e)},
)
if "chat_id" in payload: if "chat_id" in payload:
del payload["chat_id"] del payload["chat_id"]
@ -1500,10 +1518,16 @@ Message: """{{prompt}}"""
try: try:
payload = filter_pipeline(payload, user) payload = filter_pipeline(payload, user)
except Exception as e: except Exception as e:
return JSONResponse( if len(e.args) > 1:
status_code=e.args[0], return JSONResponse(
content={"detail": e.args[1]}, status_code=e.args[0],
) content={"detail": e.args[1]},
)
else:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"detail": str(e)},
)
if "chat_id" in payload: if "chat_id" in payload:
del payload["chat_id"] del payload["chat_id"]
@ -1552,10 +1576,16 @@ Responses from models: {{responses}}"""
try: try:
payload = filter_pipeline(payload, user) payload = filter_pipeline(payload, user)
except Exception as e: except Exception as e:
return JSONResponse( if len(e.args) > 1:
status_code=e.args[0], return JSONResponse(
content={"detail": e.args[1]}, status_code=e.args[0],
) content={"detail": e.args[1]},
)
else:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"detail": str(e)},
)
if "chat_id" in payload: if "chat_id" in payload:
del payload["chat_id"] del payload["chat_id"]