From 178362f6878db7935065bd03e216126b8bf6b1f4 Mon Sep 17 00:00:00 2001 From: manalabumehsen Date: Thu, 5 Feb 2026 20:55:52 +0100 Subject: [PATCH] Enhance error messages and docstring in fetch_stocks.py First contribution by Manal Updated docstring for fetch_stock_data method to include detailed argument and return information. --- Stock-Analysis/fetch_stocks.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Stock-Analysis/fetch_stocks.py b/Stock-Analysis/fetch_stocks.py index a24cb76..fedc172 100644 --- a/Stock-Analysis/fetch_stocks.py +++ b/Stock-Analysis/fetch_stocks.py @@ -6,7 +6,15 @@ class StockFetcher: @staticmethod def fetch_stock_data(stock): - """Fetch stock data from Yahoo Finance with improved error handling.""" + """ + Fetch stock data from Yahoo Finance with improved error handling. + + Args: + stock (str): Stock symbol to fetch. + + Returns: + tuple: (recommendationMean, stock) if successful, None otherwise. + """ try: ticker = yf.Ticker(stock) info = ticker.info @@ -23,12 +31,14 @@ def fetch_stock_data(stock): return (rate, stock) except ValueError as e: + print(f"ValueError: Could not fetch data for {stock}. {e}") logging.getLogger(__name__).error( f"[ValueError] Unable to fetch data for '{stock}': {e}. " f"Possible causes: Invalid stock symbol or empty API response." ) except KeyError as e: + print(f"KeyError: Missing expected data for {stock}. {e}") logging.getLogger(__name__).error( f"[KeyError] Missing expected data for '{stock}': {e}. " f"The API might have changed or the stock does not provide recommendation data.", @@ -36,16 +46,17 @@ def fetch_stock_data(stock): ) except URLError as e: + print(f"Network error: Could not fetch data for {stock}. {e}") logging.getLogger(__name__).error( f"[URLError] Network issue while fetching '{stock}': {e}. " f"Please check your internet connection or try again later." ) except Exception as e: + print(f"Oops! Could not fetch data for {stock}. Please check your stock symbol or internet connection.") logging.getLogger(__name__).error( f"[Unexpected Error] Failed to fetch '{stock}': ({type(e).__name__}) {e}", exc_info=True ) return None -