sammchardy / python-kucoin

Kucoin REST and Websocket API python implementation
https://python-kucoin.readthedocs.io/en/latest/
MIT License
349 stars 147 forks source link

502 Bad Gateway response leads to KucoinAPIException, not KucoinRequestException #31

Closed ghost closed 6 years ago

ghost commented 6 years ago

Traceback (most recent call last): File "My file which calls get_all_balances", result = ku_api.get_all_balances() File "/usr/local/lib/python3.5/dist-packages/kucoin/client.py", line 975, in get_all_balances return self._get('account/balance', True, data=data) File "/usr/local/lib/python3.5/dist-packages/kucoin/client.py", line 177, in _get return self._request('get', path, signed, **kwargs) File "/usr/local/lib/python3.5/dist-packages/kucoin/client.py", line 147, in _request return self._handle_response(response)
File "/usr/local/lib/python3.5/dist-packages/kucoin/client.py", line 156, in _handle_response
raise KucoinAPIException(response)
kucoin.exceptions.KucoinAPIException: KucoinAPIException : b'\r\n502 Bad Gateway\r\n\r\n

502 Bad Gateway

\r\n\r\n\r\n'

It seems like 502 Bad Gateway response has to raise KucoinRequestException since it is not a valid json format. I wrote a code to print out error code and error message when KucoinAPIException has been raised.

The following is how I handle KucoinAPIException

errormsg = 'KU API error with code: ' + str(e.status_code) + ' msg: ' + e.message

And at this time, it raises TypeError remarks that I cannot convert bytes to str implicitly.

sammchardy commented 6 years ago

Hi @donglesquare

Instead of concatenating strings and other types of variables it can be best to use the str format() function, your example above would become

error msg = 'KU API error with code: {} msg: {}'.format(e.status_code, e.message)