add unauthorized error handling to congress_gov (#7231)

This commit is contained in:
Danglewood
2025-10-08 08:04:26 -07:00
committed by GitHub
parent 386ab040c3
commit 88947ab5d9
2 changed files with 21 additions and 1 deletions

View File

@@ -83,6 +83,8 @@ class CongressBillInfoFetcher(
) -> dict:
"""Extract data from the query."""
# pylint: disable=import-outside-toplevel
from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.provider.utils.errors import UnauthorizedError
from openbb_core.provider.utils.helpers import amake_request
api_key = credentials.get("congress_gov_api_key", "") if credentials else ""
@@ -96,6 +98,14 @@ class CongressBillInfoFetcher(
url = bill_url + "&api_key=" + api_key
base_info: dict = await amake_request(url) # type: ignore
if isinstance(base_info, dict) and (error := base_info.get("error", {})):
if "API_KEY" in error.get("code", ""):
raise UnauthorizedError(
f"{error.get('code', '' )} -> {error.get('message', '')}"
)
raise OpenBBError(f"{error.get('code', '' )} -> {error.get('message', '')}")
base_info = base_info.get("bill", {})
cosponsors = base_info.get("cosponsors", {})

View File

@@ -212,7 +212,8 @@ class CongressBillsFetcher(
) -> list:
"""Extract data from the Congress API."""
# pylint: disable=import-outside-toplevel
from openbb_core.provider.utils.helpers import amake_request # noqa
from openbb_core.provider.utils.errors import UnauthorizedError # noqa
from openbb_core.provider.utils.helpers import amake_request
from openbb_congress_gov.utils.helpers import (
get_all_bills_by_type,
)
@@ -296,6 +297,15 @@ class CongressBillsFetcher(
try:
response = await amake_request(url=url)
if isinstance(response, dict) and (error := response.get("error", {})):
if "API_KEY" in error.get("code", ""):
raise UnauthorizedError(
f"{error.get('code', '' )} -> {error.get('message', '')}"
)
raise OpenBBError(
f"{error.get('code', '' )} -> {error.get('message', '')}"
)
except Exception as e:
# Handle exceptions
raise OpenBBError(e) from e