Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Validation Error Bug and Add Option to Disable Stacktrace in Error Messages #229

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions aana/api/exception_handler.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import traceback

from fastapi import Request
from fastapi.exceptions import RequestValidationError
from pydantic import ValidationError
from ray.exceptions import RayTaskError

from aana.api.responses import AanaJSONResponse
from aana.configs.settings import settings as aana_settings
from aana.core.models.exception import ExceptionResponseModel
from aana.exceptions.core import BaseException


async def validation_exception_handler(request: Request, exc: ValidationError):
async def validation_exception_handler(
request: Request, exc: ValidationError | RequestValidationError
):
"""This handler is used to handle pydantic validation errors.

Args:
request (Request): The request object
exc (ValidationError): The validation error
exc (ValidationError | RequestValidationError): The exception raised

Returns:
JSONResponse: JSON response with the error details
"""
if isinstance(exc, ValidationError):
data = exc.errors(include_context=False)
elif isinstance(exc, RequestValidationError):
data = exc.errors()
return AanaJSONResponse(
status_code=422,
content=ExceptionResponseModel(
error="ValidationError",
message="Validation error",
data=exc.errors(),
error="ValidationError", message="Validation error", data=data
).model_dump(),
)

Expand Down Expand Up @@ -60,6 +66,9 @@ def custom_exception_handler(request: Request | None, exc_raw: Exception):
# then we need to get the stack trace
stacktrace = traceback.format_exc()
exc = exc_raw
# Remove the stacktrace if it is disabled
if not aana_settings.include_stacktrace:
stacktrace = None
# get the data from the exception
# can be used to return additional info
# like image path, url, model name etc.
Expand Down
2 changes: 2 additions & 0 deletions aana/configs/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Settings(BaseSettings):
model_dir (Path): The temporary model directory.
num_workers (int): The number of web workers.
openai_endpoint_enabled (bool): Flag indicating if the OpenAI-compatible endpoint is enabled. Enabled by default.
include_stacktrace (bool): Flag indicating if stacktrace should be included in error messages. Enabled by default.
task_queue (TaskQueueSettings): The task queue settings.
db_config (DbSettings): The database configuration.
test (TestSettings): The test settings.
Expand All @@ -93,6 +94,7 @@ class Settings(BaseSettings):
num_workers: int = 2

openai_endpoint_enabled: bool = True
include_stacktrace: bool = True

task_queue: TaskQueueSettings = TaskQueueSettings()

Expand Down
Loading