(20241224) Validation error catching.

This commit is contained in:
2024-12-24 15:19:15 +05:30
parent 52aa5c37e4
commit eaa5431b49
@@ -65,6 +65,9 @@ import httpx
# To work with Zerodha's Kite platform: # To work with Zerodha's Kite platform:
from kiteconnect import KiteConnect from kiteconnect import KiteConnect
# To handle exceptions:
from pydantic import ValidationError
# ***************************************************************************************************************** # *****************************************************************************************************************
# ***** **** # ***** ****
@@ -265,11 +268,11 @@ class ZerodhaKiteTradingController(TradingController):
# Start by assuming failure: # Start by assuming failure:
symbol_list = TradingSymbolListBrokerResponse() symbol_list = TradingSymbolListBrokerResponse()
try:
# Fit the token into the model: # Fit the token into the model:
zerodha_token = ZerodhaKiteAuthTokens(**auth_token.token) zerodha_token = ZerodhaKiteAuthTokens(**auth_token.token)
try:
# Now create an instance of the Kite: # Now create an instance of the Kite:
kite = KiteConnect(api_key = auth_token.auth["apiKey"]) kite = KiteConnect(api_key = auth_token.auth["apiKey"])
kite.set_access_token(zerodha_token.accessToken) kite.set_access_token(zerodha_token.accessToken)
@@ -285,9 +288,11 @@ class ZerodhaKiteTradingController(TradingController):
# In case something goes wrong: # In case something goes wrong:
except Exception as exception: except Exception as exception:
symbol_list.exception = exception
symbol_list.message = str(exception)
symbol_list.data = None symbol_list.data = None
symbol_list.exception = exception
if isinstance(exception, ValidationError):
symbol_list.message = "Token error. Please log in to your Zerodha (Kite) account again."
else: symbol_list.message = str(exception)
# Done here: # Done here:
return symbol_list return symbol_list