python-binance icon indicating copy to clipboard operation
python-binance copied to clipboard

How to avoid API errors using except?

Open cc345 opened this issue 8 years ago • 9 comments

One of the errors I keep running into is this:

binance.exceptions.BinanceAPIException: APIError(code=-1000): An unknown error occured while processing the request.

I was just wondering if there is a way to pass this in a try statement? Here is what I use to pass timeout errors.

from requests.exceptions import Timeout

try:
    #Insert code here
except Timeout:
    pass

Are there any except functions like " except Timeout" for the other error codes that I could use?

cc345 avatar Apr 24 '18 01:04 cc345

you can just leave 'except' by itself

try: code except: pass

Roibal avatar Apr 24 '18 03:04 Roibal

That binance.exceptions.BinanceAPIException is the name of the exception you're asking about, actually:

try:
    stuff()
except binance.exceptions.BinanceAPIException as e:
    print(e)

wranai avatar May 01 '18 18:05 wranai

I try that and I only get "NameError: name 'binance' is not defined"

Chuox avatar Nov 04 '18 21:11 Chuox

I manage this to work

try:
    SellSta=client.get_order(symbol=Symb,orderId=SellOrderNum,recvWindow=Delay)
except Exception as e:
    print ("Eror in line: {}".format(e))
    print ("{}".format(e.status_code ))
    print ("{}".format(e.response ))
    print ("{}".format(e.code))
    print ("{}".format(e.message))
    print ("{}".format(e.request)) 

Now I'm trying to get different exceptions for differents errors

Chuox avatar Nov 28 '18 04:11 Chuox

closing because this is unrelated to lib. rather it is related to how to handle errors.

for clarity on above: @Chuox you should have imported binance like from binance import exceptions.BinanceAPIException

kimchirichie avatar Nov 06 '20 09:11 kimchirichie

closing because this is unrelated to lib. rather it is related to how to handle errors.

for clarity on above: @Chuox you should have imported binance like from binance import exceptions.BinanceAPIException

@kimchirichie No bro...

from binance.exceptions import BinanceAPIException

try:
    client.get_all_orders()
except BinanceAPIException as e:
    print(e.status_code)
    print(e.message)

dvdkitay avatar Apr 07 '22 10:04 dvdkitay

But how can I specify particular error(code)? For instance I get occasionally binance.exceptions.BinanceAPIException: APIError(code=-2010) error. How can I handle it?

Shinizlc avatar Aug 19 '22 20:08 Shinizlc

But how can I specify particular error(code)? For instance I get occasionally binance.exceptions.BinanceAPIException: APIError(code=-2010) error. How can I handle it?

@Shinizlc You can handle the error as follows:

try:
    pass
except BinanceAPIException as err:
    if err.code == -2010:
        pass
    else:
        raise err

lindomar-oliveira avatar Aug 19 '22 20:08 lindomar-oliveira

@lindomar-oliveira thank you a lot!

Shinizlc avatar Aug 19 '22 21:08 Shinizlc