From 400cfdfc94b1362d158302acfa4843088ba8d6e8 Mon Sep 17 00:00:00 2001 From: Pratyush Shukla Date: Mon, 26 Feb 2024 21:28:18 +0530 Subject: [PATCH 1/6] fix validator in fmp IndexHistorical model --- .../providers/fmp/openbb_fmp/models/index_historical.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py b/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py index 8ebf3db2f384..20be131edaa2 100644 --- a/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py +++ b/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py @@ -30,7 +30,7 @@ class FMPIndexHistoricalQueryParams(IndexHistoricalQueryParams): Field(default="1day", description="Data granularity.") ) - @field_validator("interval") + @field_validator("interval", mode="before", check_fields=True) @classmethod def map_interval(cls, v): """Map the interval from standard to the FMP format.""" From 18261cae16d91e810fd9406325476ff465474be6 Mon Sep 17 00:00:00 2001 From: Pratyush Shukla Date: Mon, 26 Feb 2024 21:29:40 +0530 Subject: [PATCH 2/6] make ValidationError pretty --- .../app/static/utils/decorators.py | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/openbb_platform/core/openbb_core/app/static/utils/decorators.py b/openbb_platform/core/openbb_core/app/static/utils/decorators.py index 3c20559d4758..423fe804ae90 100644 --- a/openbb_platform/core/openbb_core/app/static/utils/decorators.py +++ b/openbb_platform/core/openbb_core/app/static/utils/decorators.py @@ -5,7 +5,7 @@ from openbb_core.app.model.abstract.error import OpenBBError from openbb_core.env import Env -from pydantic import validate_call +from pydantic import ValidationError, validate_call from typing_extensions import ParamSpec P = ParamSpec("P") @@ -44,14 +44,42 @@ def exception_handler(func: Callable[P, R]) -> Callable[P, R]: """Handle exceptions, attempting to focus on the last call from the traceback.""" @wraps(func) - def wrapper(*args, **kwargs): + def wrapper(*f_args, **f_kwargs): try: - return func(*args, **kwargs) - except Exception as e: + return func(*f_args, **f_kwargs) + except (ValidationError, Exception) as e: if Env().DEBUG_MODE: raise - raise OpenBBError( - f"\nType -> {e.__class__.__name__}\n\nDetail -> {str(e)}" - ) from None + if isinstance(e, ValidationError): + idx = 0 + input_value = "" + error_list = [] + + validation_error = f"{e.error_count()} validations errors in {e.title}" + for error in e.errors(): + if error["input"] == input_value: + idx -= 1 + idx += 1 + input_value = error["input"] + + arg_error = f"Arg {idx} -> {error['loc'][0]}\n" + error_details = ( + f" {error['msg']} " + f"[validation_error_type={error['type']}, " + f"input_type={type(error['input']).__name__}, " + f"input_value={input_value}]\n" + ) + error_info = f" For further information visit {error['url']}\n" + error_list.append(arg_error + error_details + error_info) + + error_list.insert(0, validation_error) + error_str = "\n".join(error_list) + raise OpenBBError( + f"\nType -> ValidationError \n\nDetails -> {error_str}" + ) from None + else: + raise OpenBBError( + f"\nType -> {e.original.original.__class__.__name__}\n\nDetail -> {str(e)}" + ) from None return wrapper From d8508063c5b920177daa51a664c4cae7fdda488f Mon Sep 17 00:00:00 2001 From: Pratyush Shukla Date: Mon, 26 Feb 2024 23:50:52 +0530 Subject: [PATCH 3/6] modify arg error --- openbb_platform/core/openbb_core/app/static/utils/decorators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbb_platform/core/openbb_core/app/static/utils/decorators.py b/openbb_platform/core/openbb_core/app/static/utils/decorators.py index 423fe804ae90..fa8b9e33d746 100644 --- a/openbb_platform/core/openbb_core/app/static/utils/decorators.py +++ b/openbb_platform/core/openbb_core/app/static/utils/decorators.py @@ -62,7 +62,7 @@ def wrapper(*f_args, **f_kwargs): idx += 1 input_value = error["input"] - arg_error = f"Arg {idx} -> {error['loc'][0]}\n" + arg_error = f"Arg {error['loc'][0]} ->\n" error_details = ( f" {error['msg']} " f"[validation_error_type={error['type']}, " From 736d9d77f4d4cb9a9df0ead99305f567d8a84e84 Mon Sep 17 00:00:00 2001 From: Pratyush Shukla Date: Tue, 27 Feb 2024 02:42:42 +0530 Subject: [PATCH 4/6] linting fmp IndexHistorical --- .../fmp/openbb_fmp/models/index_historical.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py b/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py index 20be131edaa2..fcdc837564fa 100644 --- a/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py +++ b/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py @@ -26,9 +26,9 @@ class FMPIndexHistoricalQueryParams(IndexHistoricalQueryParams): timeseries: Optional[NonNegativeInt] = Field( default=None, description="Number of days to look back." ) - interval: Literal["1min", "5min", "15min", "30min", "1hour", "4hour", "1day"] = ( - Field(default="1day", description="Data granularity.") - ) + interval: Literal[ + "1min", "5min", "15min", "30min", "1hour", "4hour", "1day" + ] = Field(default="1day", description="Data granularity.") @field_validator("interval", mode="before", check_fields=True) @classmethod @@ -104,9 +104,12 @@ async def aextract_data( return await get_data_many(url, "historical", **kwargs) + # pylint: disable=unused-argument @staticmethod def transform_data( - query: FMPIndexHistoricalQueryParams, data: List[Dict], **kwargs: Any + query: FMPIndexHistoricalQueryParams, + data: List[Dict], + **kwargs: Any, ) -> List[FMPIndexHistoricalData]: """Return the transformed data.""" return [ From 6d2f7c30f4a106f41d8bf2cbd808732af8ba7b3b Mon Sep 17 00:00:00 2001 From: Pratyush Shukla Date: Tue, 27 Feb 2024 02:47:08 +0530 Subject: [PATCH 5/6] black --- .../providers/fmp/openbb_fmp/models/index_historical.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py b/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py index fcdc837564fa..087f6fa15f77 100644 --- a/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py +++ b/openbb_platform/providers/fmp/openbb_fmp/models/index_historical.py @@ -26,9 +26,9 @@ class FMPIndexHistoricalQueryParams(IndexHistoricalQueryParams): timeseries: Optional[NonNegativeInt] = Field( default=None, description="Number of days to look back." ) - interval: Literal[ - "1min", "5min", "15min", "30min", "1hour", "4hour", "1day" - ] = Field(default="1day", description="Data granularity.") + interval: Literal["1min", "5min", "15min", "30min", "1hour", "4hour", "1day"] = ( + Field(default="1day", description="Data granularity.") + ) @field_validator("interval", mode="before", check_fields=True) @classmethod From 4c3d697d1dfb9b32c3261a90cb33a3a82c95c094 Mon Sep 17 00:00:00 2001 From: Pratyush Shukla Date: Tue, 27 Feb 2024 03:37:47 +0530 Subject: [PATCH 6/6] remove unused code --- .../openbb_core/app/static/utils/decorators.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/openbb_platform/core/openbb_core/app/static/utils/decorators.py b/openbb_platform/core/openbb_core/app/static/utils/decorators.py index fa8b9e33d746..7a9af5ead87e 100644 --- a/openbb_platform/core/openbb_core/app/static/utils/decorators.py +++ b/openbb_platform/core/openbb_core/app/static/utils/decorators.py @@ -51,23 +51,16 @@ def wrapper(*f_args, **f_kwargs): if Env().DEBUG_MODE: raise if isinstance(e, ValidationError): - idx = 0 - input_value = "" error_list = [] validation_error = f"{e.error_count()} validations errors in {e.title}" for error in e.errors(): - if error["input"] == input_value: - idx -= 1 - idx += 1 - input_value = error["input"] - arg_error = f"Arg {error['loc'][0]} ->\n" error_details = ( f" {error['msg']} " f"[validation_error_type={error['type']}, " f"input_type={type(error['input']).__name__}, " - f"input_value={input_value}]\n" + f"input_value={error['input']}]\n" ) error_info = f" For further information visit {error['url']}\n" error_list.append(arg_error + error_details + error_info) @@ -77,9 +70,10 @@ def wrapper(*f_args, **f_kwargs): raise OpenBBError( f"\nType -> ValidationError \n\nDetails -> {error_str}" ) from None - else: - raise OpenBBError( - f"\nType -> {e.original.original.__class__.__name__}\n\nDetail -> {str(e)}" - ) from None + + # If the error is not a ValidationError, then it is a generic exception + raise OpenBBError( + f"\nType -> {e.original.original.__class__.__name__}\n\nDetail -> {str(e)}" + ) from None return wrapper