Compare commits

...
Sign in to create a new pull request.

6 commits

Author SHA1 Message Date
Daulet Amirkhanov
fd2a629971 remove previous attempt at centralized Exception handling 2025-10-29 19:21:53 +00:00
Daulet Amirkhanov
6c180891f9
Merge branch 'dev' into chore/improve-error-logging 2025-10-29 19:19:45 +00:00
Daulet Amirkhanov
8e4cdf4334
Potential fix for code scanning alert no. 397: Information exposure through an exception
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
2025-10-29 19:19:22 +00:00
Daulet Amirkhanov
4977491966 feat: Add global and middleware exception handling for improved error logging 2025-10-29 19:17:13 +00:00
Daulet Amirkhanov
bcfae9f291 chore: remove exc_info 2025-10-29 19:06:46 +00:00
Daulet Amirkhanov
440b4b0cf3 fix: Improve error handling in add operation 2025-10-29 18:01:51 +00:00
2 changed files with 30 additions and 17 deletions

View file

@ -80,6 +80,24 @@ async def lifespan(app: FastAPI):
app = FastAPI(debug=app_environment != "prod", lifespan=lifespan) app = FastAPI(debug=app_environment != "prod", lifespan=lifespan)
@app.middleware("http")
async def log_exceptions_middleware(request: Request, call_next):
try:
return await call_next(request)
except Exception as exc:
logger.error(
f"Unhandled exception in {request.method} {request.url.path}: "
f"{type(exc).__name__}: {str(exc)}"
)
return JSONResponse(
status_code=500,
content={
"message": "An internal server error occurred. Please check server logs for details.",
"path": str(request.url.path),
},
)
# Read allowed origins from environment variable (comma-separated) # Read allowed origins from environment variable (comma-separated)
CORS_ALLOWED_ORIGINS = os.getenv("CORS_ALLOWED_ORIGINS") CORS_ALLOWED_ORIGINS = os.getenv("CORS_ALLOWED_ORIGINS")
if CORS_ALLOWED_ORIGINS: if CORS_ALLOWED_ORIGINS:

View file

@ -9,11 +9,8 @@ from cognee.modules.users.models import User
from cognee.modules.users.methods import get_authenticated_user from cognee.modules.users.methods import get_authenticated_user
from cognee.shared.utils import send_telemetry from cognee.shared.utils import send_telemetry
from cognee.modules.pipelines.models import PipelineRunErrored from cognee.modules.pipelines.models import PipelineRunErrored
from cognee.shared.logging_utils import get_logger
from cognee import __version__ as cognee_version from cognee import __version__ as cognee_version
logger = get_logger()
def get_add_router() -> APIRouter: def get_add_router() -> APIRouter:
router = APIRouter() router = APIRouter()
@ -54,7 +51,8 @@ def get_add_router() -> APIRouter:
## Error Codes ## Error Codes
- **400 Bad Request**: Neither datasetId nor datasetName provided - **400 Bad Request**: Neither datasetId nor datasetName provided
- **409 Conflict**: Error during add operation - **500 Internal Server Error**: Error during add operation (check logs for details)
- **420 Pipeline Error**: Pipeline execution failed with structured error details
- **403 Forbidden**: User doesn't have permission to add to dataset - **403 Forbidden**: User doesn't have permission to add to dataset
## Notes ## Notes
@ -76,7 +74,6 @@ def get_add_router() -> APIRouter:
if not datasetId and not datasetName: if not datasetId and not datasetName:
raise ValueError("Either datasetId or datasetName must be provided.") raise ValueError("Either datasetId or datasetName must be provided.")
try:
add_run = await cognee_add( add_run = await cognee_add(
data, data,
datasetName, datasetName,
@ -88,7 +85,5 @@ def get_add_router() -> APIRouter:
if isinstance(add_run, PipelineRunErrored): if isinstance(add_run, PipelineRunErrored):
return JSONResponse(status_code=420, content=add_run.model_dump(mode="json")) return JSONResponse(status_code=420, content=add_run.model_dump(mode="json"))
return add_run.model_dump() return add_run.model_dump()
except Exception as error:
return JSONResponse(status_code=409, content={"error": str(error)})
return router return router