From eaa5431b49ba78b68d43baf7a69988337348611e Mon Sep 17 00:00:00 2001 From: khushal Date: Tue, 24 Dec 2024 15:19:15 +0530 Subject: [PATCH] (20241224) Validation error catching. --- .../finstitutions/trading/zerodha_kite.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/controllers_v2/finstitutions/trading/zerodha_kite.py b/controllers_v2/finstitutions/trading/zerodha_kite.py index e470f32..7a0e3f1 100644 --- a/controllers_v2/finstitutions/trading/zerodha_kite.py +++ b/controllers_v2/finstitutions/trading/zerodha_kite.py @@ -65,6 +65,9 @@ import httpx # To work with Zerodha's Kite platform: from kiteconnect import KiteConnect +# To handle exceptions: +from pydantic import ValidationError + # ***************************************************************************************************************** # ***** **** @@ -265,11 +268,11 @@ class ZerodhaKiteTradingController(TradingController): # Start by assuming failure: symbol_list = TradingSymbolListBrokerResponse() - # Fit the token into the model: - zerodha_token = ZerodhaKiteAuthTokens(**auth_token.token) - try: + # Fit the token into the model: + zerodha_token = ZerodhaKiteAuthTokens(**auth_token.token) + # Now create an instance of the Kite: kite = KiteConnect(api_key = auth_token.auth["apiKey"]) kite.set_access_token(zerodha_token.accessToken) @@ -285,9 +288,11 @@ class ZerodhaKiteTradingController(TradingController): # In case something goes wrong: except Exception as exception: - symbol_list.exception = exception - symbol_list.message = str(exception) 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: return symbol_list