mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-08-03 13:43:18 +02:00
logging improvements
This commit is contained in:
committed by
Chris Weaver
parent
8d443ada5b
commit
a96cea2ce0
@@ -1,4 +1,5 @@
|
|||||||
import time
|
import time
|
||||||
|
import traceback
|
||||||
from collections.abc import AsyncGenerator
|
from collections.abc import AsyncGenerator
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@@ -8,6 +9,7 @@ import uvicorn
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi import Request
|
from fastapi import Request
|
||||||
|
from fastapi import status
|
||||||
from fastapi.exceptions import RequestValidationError
|
from fastapi.exceptions import RequestValidationError
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
@@ -407,11 +409,32 @@ async def lifespan(app: FastAPI) -> AsyncGenerator:
|
|||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
def log_http_error(_: Request, exc: Exception) -> JSONResponse:
|
||||||
|
status_code = getattr(exc, "status_code", 500)
|
||||||
|
if status_code >= 400:
|
||||||
|
error_msg = f"{str(exc)}\n"
|
||||||
|
error_msg += "".join(traceback.format_tb(exc.__traceback__))
|
||||||
|
logger.error(error_msg)
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=status_code,
|
||||||
|
content={"detail": str(exc)},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_application() -> FastAPI:
|
def get_application() -> FastAPI:
|
||||||
application = FastAPI(
|
application = FastAPI(
|
||||||
title="Danswer Backend", version=__version__, lifespan=lifespan
|
title="Danswer Backend", version=__version__, lifespan=lifespan
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Add the custom exception handler
|
||||||
|
application.add_exception_handler(status.HTTP_400_BAD_REQUEST, log_http_error)
|
||||||
|
application.add_exception_handler(status.HTTP_401_UNAUTHORIZED, log_http_error)
|
||||||
|
application.add_exception_handler(status.HTTP_403_FORBIDDEN, log_http_error)
|
||||||
|
application.add_exception_handler(status.HTTP_404_NOT_FOUND, log_http_error)
|
||||||
|
application.add_exception_handler(
|
||||||
|
status.HTTP_500_INTERNAL_SERVER_ERROR, log_http_error
|
||||||
|
)
|
||||||
|
|
||||||
include_router_with_global_prefix_prepended(application, chat_router)
|
include_router_with_global_prefix_prepended(application, chat_router)
|
||||||
include_router_with_global_prefix_prepended(application, query_router)
|
include_router_with_global_prefix_prepended(application, query_router)
|
||||||
include_router_with_global_prefix_prepended(application, document_router)
|
include_router_with_global_prefix_prepended(application, document_router)
|
||||||
|
@@ -69,7 +69,7 @@ def set_user_role(
|
|||||||
|
|
||||||
if user_role_update_request.new_role == UserRole.CURATOR:
|
if user_role_update_request.new_role == UserRole.CURATOR:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=402,
|
status_code=400,
|
||||||
detail="Curator role must be set via the User Group Menu",
|
detail="Curator role must be set via the User Group Menu",
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -78,7 +78,7 @@ def set_user_role(
|
|||||||
|
|
||||||
if current_user.id == user_to_update.id:
|
if current_user.id == user_to_update.id:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=402,
|
status_code=400,
|
||||||
detail="An admin cannot demote themselves from admin role!",
|
detail="An admin cannot demote themselves from admin role!",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -51,14 +51,14 @@ def validate_user_creation_permissions(
|
|||||||
detail = "User does not have permission to create public credentials"
|
detail = "User does not have permission to create public credentials"
|
||||||
logger.error(detail)
|
logger.error(detail)
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=402,
|
status_code=400,
|
||||||
detail=detail,
|
detail=detail,
|
||||||
)
|
)
|
||||||
if not target_group_ids:
|
if not target_group_ids:
|
||||||
detail = "Curators must specify 1+ groups"
|
detail = "Curators must specify 1+ groups"
|
||||||
logger.error(detail)
|
logger.error(detail)
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=402,
|
status_code=400,
|
||||||
detail=detail,
|
detail=detail,
|
||||||
)
|
)
|
||||||
user_curated_groups = fetch_user_groups_for_user(
|
user_curated_groups = fetch_user_groups_for_user(
|
||||||
@@ -70,7 +70,7 @@ def validate_user_creation_permissions(
|
|||||||
detail = "Curators cannot control groups they don't curate"
|
detail = "Curators cannot control groups they don't curate"
|
||||||
logger.error(detail)
|
logger.error(detail)
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=402,
|
status_code=400,
|
||||||
detail=detail,
|
detail=detail,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user