T-K-O-H commited on
Commit
59f3ecb
·
1 Parent(s): fa43227

Improved error handling for invalid tickers in tools.py

Browse files
Files changed (1) hide show
  1. tools.py +14 -3
tools.py CHANGED
@@ -10,7 +10,13 @@ def get_price(ticker):
10
  stock = yf.Ticker(ticker)
11
 
12
  # Get the stock info
13
- info = stock.info
 
 
 
 
 
 
14
  if 'regularMarketPrice' not in info or info['regularMarketPrice'] is None:
15
  return f"Could not get the current price for {ticker}. Please verify the ticker symbol."
16
 
@@ -41,8 +47,13 @@ def buying_power_tool(query):
41
 
42
  # Get stock price
43
  stock = yf.Ticker(ticker)
44
- info = stock.info
45
-
 
 
 
 
 
46
  if 'regularMarketPrice' not in info or info['regularMarketPrice'] is None:
47
  return f"Could not get the current price for {ticker}. Please verify the ticker symbol."
48
 
 
10
  stock = yf.Ticker(ticker)
11
 
12
  # Get the stock info
13
+ try:
14
+ info = stock.info
15
+ except Exception as e:
16
+ if "404" in str(e):
17
+ return f"'{ticker}' is not a valid stock ticker. Please check the symbol and try again."
18
+ return f"Error fetching data for {ticker}: {str(e)}"
19
+
20
  if 'regularMarketPrice' not in info or info['regularMarketPrice'] is None:
21
  return f"Could not get the current price for {ticker}. Please verify the ticker symbol."
22
 
 
47
 
48
  # Get stock price
49
  stock = yf.Ticker(ticker)
50
+ try:
51
+ info = stock.info
52
+ except Exception as e:
53
+ if "404" in str(e):
54
+ return f"'{ticker}' is not a valid stock ticker. Please check the symbol and try again."
55
+ return f"Error fetching data for {ticker}: {str(e)}"
56
+
57
  if 'regularMarketPrice' not in info or info['regularMarketPrice'] is None:
58
  return f"Could not get the current price for {ticker}. Please verify the ticker symbol."
59