Skip to content

Commit

Permalink
[BugFix] API: Handle EmptyDataError As Status Code 204 (#6682)
Browse files Browse the repository at this point in the history
* handle EmptyDataError as status code 204

* cleanupP

* unused argument

---------

Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
  • Loading branch information
deeleeramone and IgorWounds authored Oct 3, 2024
1 parent fe16bc3 commit c786852
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions openbb_platform/core/openbb_core/api/app_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from openbb_core.api.exception_handlers import ExceptionHandlers
from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.app.router import RouterLoader
from openbb_core.provider.utils.errors import EmptyDataError
from pydantic import ValidationError


Expand Down Expand Up @@ -38,3 +39,4 @@ def add_exception_handlers(app: FastAPI):
app.exception_handlers[Exception] = ExceptionHandlers.exception
app.exception_handlers[ValidationError] = ExceptionHandlers.validation
app.exception_handlers[OpenBBError] = ExceptionHandlers.openbb
app.exception_handlers[EmptyDataError] = ExceptionHandlers.empty_data
10 changes: 9 additions & 1 deletion openbb_platform/core/openbb_core/api/exception_handlers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""Exception handlers module."""

# pylint: disable=unused-argument

import logging
from collections.abc import Iterable
from typing import Any

from fastapi import Request
from fastapi.responses import JSONResponse
from fastapi.responses import JSONResponse, Response
from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.env import Env
from openbb_core.provider.utils.errors import EmptyDataError
from pydantic import ValidationError

logger = logging.getLogger("uvicorn.error")
Expand Down Expand Up @@ -91,3 +94,8 @@ async def openbb(_: Request, error: OpenBBError):
status_code=400,
detail=str(error.original),
)

@staticmethod
async def empty_data(_: Request, error: EmptyDataError):
"""Exception handler for EmptyDataError."""
return Response(status_code=204)
3 changes: 3 additions & 0 deletions openbb_platform/core/openbb_core/app/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ def command(
kwargs["responses"] = kwargs.get(
"responses",
{
204: {
"description": "Empty response",
},
400: {
"model": OpenBBErrorResponse,
"description": "No Results Found",
Expand Down

0 comments on commit c786852

Please sign in to comment.