replace provider error by openbb (#5812)

This commit is contained in:
montezdesousa
2023-11-28 19:19:21 +00:00
committed by GitHub
parent 06f0e7257e
commit ed2fc04814
4 changed files with 16 additions and 19 deletions

View File

@@ -3,10 +3,10 @@ from typing import Any, Dict, Optional, Type
from pydantic import SecretStr
from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.provider.abstract.fetcher import Fetcher
from openbb_core.provider.abstract.provider import Provider
from openbb_core.provider.registry import Registry, RegistryLoader
from openbb_core.provider.utils.errors import ProviderError
class QueryExecutor:
@@ -20,7 +20,7 @@ class QueryExecutor:
"""Get a provider from the registry."""
name = provider_name.lower()
if name not in self.registry.providers:
raise ProviderError(
raise OpenBBError(
f"Provider '{name}' not found in the registry."
f"Available providers: {list(self.registry.providers.keys())}"
)
@@ -29,7 +29,7 @@ class QueryExecutor:
def get_fetcher(self, provider: Provider, model_name: str) -> Type[Fetcher]:
"""Get a fetcher from a provider."""
if model_name not in provider.fetcher_dict:
raise ProviderError(
raise OpenBBError(
f"Fetcher not found for model '{model_name}' in provider '{provider.name}'."
)
return provider.fetcher_dict[model_name]
@@ -53,7 +53,7 @@ class QueryExecutor:
if require_credentials:
website = provider.website or ""
extra_msg = f"Check {website} to get it." if website else ""
raise ProviderError(f"Missing credential '{c}'. {extra_msg}")
raise OpenBBError(f"Missing credential '{c}'. {extra_msg}")
else:
filtered_credentials[c] = credential_value.get_secret_value()
@@ -95,4 +95,4 @@ class QueryExecutor:
try:
return await fetcher.fetch_data(params, filtered_credentials, **kwargs)
except Exception as e:
raise ProviderError(e) from e
raise OpenBBError(e) from e

View File

@@ -1,8 +1,4 @@
""" Custom exceptions for the provider. """
class ProviderError(Exception):
"""Exception raised for errors in the provider."""
"""Custom exceptions for the provider."""
class EmptyDataError(Exception):

View File

@@ -4,10 +4,10 @@
from unittest.mock import MagicMock, patch
import pytest
from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.provider.abstract.fetcher import Fetcher
from openbb_core.provider.abstract.provider import Provider
from openbb_core.provider.query_executor import QueryExecutor
from openbb_core.provider.utils.errors import ProviderError
from pydantic import SecretStr
@@ -34,7 +34,7 @@ def test_get_provider_success(mock_query_executor):
def test_get_provider_failure(mock_query_executor):
"""Test if the method fails properly when the provider does not exist."""
with pytest.raises(ProviderError, match="Provider 'nonexistent' not found"):
with pytest.raises(OpenBBError, match="Provider 'nonexistent' not found"):
mock_query_executor.get_provider("nonexistent")
@@ -48,7 +48,7 @@ def test_get_fetcher_success(mock_query_executor):
def test_get_fetcher_failure(mock_query_executor):
"""Test if the method fails properly when the fetcher does not exist."""
provider = mock_query_executor.get_provider("test_provider")
with pytest.raises(ProviderError, match="Fetcher not found"):
with pytest.raises(OpenBBError, match="Fetcher not found"):
mock_query_executor.get_fetcher(provider, "nonexistent_fetcher")
@@ -74,7 +74,7 @@ def test_filter_credentials_missing_require(mock_query_executor):
provider.credentials = ["test_provider_api_key"]
credentials = {"other_api_key": SecretStr("12345")}
with pytest.raises(ProviderError, match="Missing credential"):
with pytest.raises(OpenBBError, match="Missing credential"):
mock_query_executor.filter_credentials(credentials, provider, True)

View File

@@ -1,12 +1,13 @@
"""Test custom errors."""
import pytest
from openbb_core.provider.utils.errors import EmptyDataError, ProviderError
from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.provider.utils.errors import EmptyDataError
def function_that_raises_provider_error():
"""Raise a ProviderError."""
raise ProviderError("An error occurred in the provider.")
"""Raise a OpenBBError."""
raise OpenBBError("An error occurred in the provider.")
def function_that_raises_empty_data_error():
@@ -15,8 +16,8 @@ def function_that_raises_empty_data_error():
def test_provider_error_is_raised():
"""Test if the ProviderError is raised."""
with pytest.raises(ProviderError) as exc_info:
"""Test if the OpenBBError is raised."""
with pytest.raises(OpenBBError) as exc_info:
function_that_raises_provider_error()
assert str(exc_info.value) == "An error occurred in the provider."