From 6fa85372c6ff244e451afa4bc8e341f8d9e91d8c Mon Sep 17 00:00:00 2001 From: Colin Delahunty <72827203+colin99d@users.noreply.github.com> Date: Wed, 19 Oct 2022 17:09:44 +0200 Subject: [PATCH] Added error handling (#2922) * Added error handling * Added fixes --- .../mutual_funds/investpy_model.py | 78 ++++++++++++++----- openbb_terminal/mutual_funds/investpy_view.py | 4 + 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/openbb_terminal/mutual_funds/investpy_model.py b/openbb_terminal/mutual_funds/investpy_model.py index 8477c767c96..aa677bb4c00 100644 --- a/openbb_terminal/mutual_funds/investpy_model.py +++ b/openbb_terminal/mutual_funds/investpy_model.py @@ -8,6 +8,7 @@ from typing import Tuple import investpy import investiny import pandas as pd +from requests.exceptions import RequestException from openbb_terminal.decorators import log_start_end from openbb_terminal.rich_config import console @@ -36,6 +37,12 @@ def search_funds(by: str = "name", value: str = "") -> pd.DataFrame: except RuntimeError as e: logger.exception(str(e)) return pd.DataFrame() + except RequestException: + console.print("[red]The request to the website failed[/red]\n") + return pd.DataFrame() + except ConnectionError: + console.print("[red]The request to the website failed[/red]\n") + return pd.DataFrame() @log_start_end(log=logger) @@ -54,9 +61,16 @@ def get_overview(country: str = "united states", limit: int = 20) -> pd.DataFram pd.DataFrame Dataframe containing overview """ - return investpy.funds.get_funds_overview( - country=country, as_json=False, n_results=limit - ) + try: + return investpy.funds.get_funds_overview( + country=country, as_json=False, n_results=limit + ) + except RequestException: + console.print("[red]The request to the website failed[/red]\n") + return pd.DataFrame() + except ConnectionError: + console.print("[red]The request to the website failed[/red]\n") + return pd.DataFrame() @log_start_end(log=logger) @@ -75,7 +89,14 @@ def get_fund_symbol_from_name(name: str) -> Tuple[str, str]: str Country in which matching symbol was found """ - name_search_results = investpy.search_funds(by="name", value=name) + try: + name_search_results = investpy.search_funds(by="name", value=name) + except RequestException: + console.print("[red]The request to the website failed[/red]\n") + return "", "" + except ConnectionError: + console.print("[red]The request to the website failed[/red]\n") + return "", "" if name_search_results.empty: return "", "" symbol = name_search_results.loc[:, "symbol"][0] @@ -102,7 +123,14 @@ def get_fund_name_from_symbol(symbol: str) -> Tuple[str, str]: str Country matching symbol """ - symbol_search_results = investpy.search_funds(by="symbol", value=symbol) + try: + symbol_search_results = investpy.search_funds(by="symbol", value=symbol) + except RequestException: + console.print("[red]The request to the website failed[/red]\n") + return "", "" + except ConnectionError: + console.print("[red]The request to the website failed[/red]\n") + return "", "" if symbol_search_results.empty: return "", "" name = symbol_search_results.loc[:, "name"][0] @@ -129,7 +157,14 @@ def get_fund_info(name: str, country: str = "united states") -> pd.DataFrame: pd.DataFrame Dataframe of fund information """ - return investpy.funds.get_fund_information(name, country).T + try: + return investpy.funds.get_fund_information(name, country).T + except RequestException: + console.print("[red]The request to the website failed[/red]\n") + return pd.DataFrame() + except ConnectionError: + console.print("[red]The request to the website failed[/red]\n") + return pd.DataFrame() @log_start_end(log=logger) @@ -166,20 +201,23 @@ def get_fund_historical( str: Country that matches search results """ - if by_name: - fund_name = name - try: + try: + if by_name: + fund_name = name fund_symbol, matching_country = get_fund_symbol_from_name(name) - except RuntimeError as e: - logger.exception(str(e)) - return pd.DataFrame(), name, "", country - else: - fund_symbol = name - try: + else: + fund_symbol = name fund_name, matching_country = get_fund_name_from_symbol(name) - except RuntimeError as e: - logger.exception(str(e)) - return pd.DataFrame(), "", name, country + + except RuntimeError as e: + logger.exception(str(e)) + return pd.DataFrame(), name, "", country + except RequestException: + console.print("[red]The request to the website failed[/red]\n") + return pd.DataFrame(), "", "", "" + except ConnectionError: + console.print("[red]The request to the website failed[/red]\n") + return pd.DataFrame(), "", "", "" # Note that dates for investpy need to be in the format mm/dd/yyyy from_date = start_date.strftime("%m/%d/%Y") @@ -202,6 +240,10 @@ def get_fund_historical( console.print("[red]Error connecting to the data source.[/red]\n") logger.exception(str(e)) return pd.DataFrame(), fund_name, fund_symbol, search_country + except RequestException as e: + console.print("[red]Error connecting to the data source.[/red]\n") + logger.exception(str(e)) + return pd.DataFrame(), fund_name, fund_symbol, search_country except ConnectionError as e: console.print("[red]Error connecting to the data source.[/red]\n") logger.exception(str(e)) diff --git a/openbb_terminal/mutual_funds/investpy_view.py b/openbb_terminal/mutual_funds/investpy_view.py index 94d42f079b3..44ca4bf509c 100644 --- a/openbb_terminal/mutual_funds/investpy_view.py +++ b/openbb_terminal/mutual_funds/investpy_view.py @@ -84,6 +84,8 @@ def display_overview(country: str = "united states", limit: int = 10, export: st Format to export data """ overview = investpy_model.get_overview(country=country, limit=limit) + if overview.empty: + return overview["Assets (1B)"] = overview.total_assets / 1_000_000_000 overview = overview.drop(columns=["country", "total_assets"]) print_rich_table( @@ -115,6 +117,8 @@ def display_fund_info(name: str, country: str = "united states"): .applymap(lambda x: np.nan if not x else x) .dropna() ) + if info.empty: + return # redact inception date if it appears castable to a float try: