Hi I'm trying to handle an exception thrown by Binance and output from the function the error so I can let the user know the API details didn't work
Source: https://github.com/binance/binance-connector-python
I get this error already on the line for try:
'Spot' object has no attribute 'ClientError'
Unclear for me how to handle the ClientError thrown by Binance. I tried other combinations like adding binance.error.ClientError
Code below
import requests
import json
from binance.spot import Spot
def verify_api_key(api_key, api_secret):
# api key/secret are required for user data endpoints
client = Spot(key=api_key, secret=api_secret)
# Get account and balance information
try:
return client.api_key_permissions()
except client.ClientError as e:
raise e
print(verify_api_key("test","test"))
How the Error thrown by Binance looks like
raise ClientError(status_code, err["code"], err["msg"], response.headers)
binance.error.ClientError: (400, -2008, 'Invalid Api-Key ID.', {'Content-Type': 'application/json;charset=UTF-8', 'Content-Length': '42', 'Connection': 'keep-alive', 'Date': 'Wed, 16 Mar 2022 20:23:04 GMT', 'Server': 'nginx', 'Strict-Transport-Security': 'max-age=31536000; includeSubdomains', 'X-Frame-Options': 'SAMEORIGIN', 'X-Xss-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'Content-Security-Policy': "default-src 'self'", 'X-Content-Security-Policy': "default-src 'self'", 'X-WebKit-CSP': "default-src 'self'", 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'X-Cache': 'Error from cloudfront', 'Via': '1.1 3ddbbcaacc1ba68ddfab04ef45c3ca98.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'MUC50-P1', 'X-Amz-Cf-Id': 'hBKLvd1lmcWNrI97jwGEnT2PH0jOXlvkMOkdfftTkAhqEkFEx8Xdaw=='})
CodePudding user response:
Reading the binance documentation it shows the proper way to reference the ClientError is binance.error.ClientError
. You said in your question that you tried this, but in the provided code you only imported binance.spot
- that isnt going to include importing ClientError.
Try importing the entire binance package with import binance
and then try using binance.error.ClientError
again. This should work for you.