Kite client unable to capture margin error.

ArnavSingh
In the kite connect source code, there is no exception for margin error. So, when a margin error occurs it is classified as GeneralException.
When we print the actual response we get this:
{'status': 'error', 'message': 'Insufficient funds. Required margin is 251037.98 but available margin is 250226.40.', 'data': None, 'error_type': 'MarginException'}

The error_type: MarginException is not handled properly in the source code:
```
# connect.py, function: _request
# api error
if data.get("status") == "error" or data.get("error_type"):
# Call session hook if its registered and TokenException is raised
if self.session_expiry_hook and r.status_code == 403 and data["error_type"] == "TokenException":
self.session_expiry_hook()

# native Kite errors
exp = getattr(ex, data.get("error_type"), ex.GeneralException)
print(data)
raise exp(data["message"], code=r.status_code)
```

This is because margin error is not defined here, only the following exceptions are there:
```
# exceptions.py
class KiteException(Exception):
class GeneralException(KiteException):
class TokenException(KiteException):
class PermissionException(KiteException):
class OrderException(KiteException):
class InputException(KiteException):
class DataException(KiteException):
class NetworkException(KiteException):
```

Currently, its raised as GeneralException, so we get a log like:
kiteconnect.exceptions.GeneralException: Insufficient funds. Required margin is 251037.98 but available margin is 250226.40.
We have to parse the error string to know if its a margin error. This approach is very fragile.
If its raised as a MarginException, then the error handling becomes robust, and we can get a signal to for example exit all our positions.
Therefore, I would request the developer team, to add a MarginException class, so that it becomes easier to catch order error.
Thank you.
Sign In or Register to comment.