Skip to content

Commit

Permalink
clear error logging for bitfinex gzipped responses ccxt#441
Browse files Browse the repository at this point in the history
  • Loading branch information
kroitor committed Nov 2, 2017
1 parent ae5a5de commit 328adb8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
39 changes: 13 additions & 26 deletions python/ccxt/base/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,62 +278,49 @@ def fetch(self, url, method='GET', headers=None, body=None):
opener = _urllib.build_opener(handler)
response = opener.open(request, timeout=int(self.timeout / 1000))
text = response.read()
text = self.gzip_deflate(response, text)
text = text.decode('utf-8')
self.last_http_response = text
except socket.timeout as e:
raise RequestTimeout(' '.join([self.id, method, url, 'request timeout']))
except ssl.SSLError as e:
self.raise_error(ExchangeNotAvailable, url, method, e)
except _urllib.HTTPError as e:
message = e.read()
message = self.gzip_deflate(e, e.read())
try:
message = message.decode('utf-8')
except UnicodeError:
pass
self.handle_errors(e.code, e.reason, url, method, None, message)
self.handle_errors(e.code, e.reason, url, method, None, message if message else text)
self.handle_rest_errors(e, e.code, message if message else text, url, method)
self.raise_error(ExchangeError, url, method, e, text if text else None)
self.raise_error(ExchangeError, url, method, e, message if message else text)
except _urllib.URLError as e:
self.raise_error(ExchangeNotAvailable, url, method, e)
except httplib.BadStatusLine as e:
self.raise_error(ExchangeNotAvailable, url, method, e)
text = self.gzip_deflate(response, text)
decoded_text = text.decode('utf-8')
self.last_http_response = decoded_text
if self.verbose:
print(method, url, "\nResponse:", str(response.info()), decoded_text)
return self.handle_rest_response(decoded_text, url, method, headers, body)
print(method, url, "\nResponse:", str(response.info()), text)
return self.handle_rest_response(text, url, method, headers, body)

def handle_rest_errors(self, exception, http_status_code, response, url, method='GET'):
error = None
details = response if response else None
if http_status_code == 429:
error = DDoSProtection
elif http_status_code in [404, 409, 422, 500, 501, 502, 520, 521, 522, 525]:
details = exception.read().decode('utf-8', 'ignore') if exception else (str(http_status_code) + ' ' + response)
error = ExchangeNotAvailable
elif http_status_code in [400, 403, 405, 503, 530]:
# special case to detect ddos protection
reason = exception.read().decode('utf-8', 'ignore') if exception else response
ddos_protection = re.search('(cloudflare|incapsula)', reason, flags=re.IGNORECASE)
if ddos_protection:
error = DDoSProtection
else:
error = ExchangeNotAvailable
details = '(possible reasons: ' + ', '.join([
'invalid API keys',
'bad or old nonce',
'exchange is down or offline',
'on maintenance',
'DDoS protection',
'rate-limiting',
reason,
]) + ')'
error = ExchangeNotAvailable
if response:
ddos_protection = re.search('(cloudflare|incapsula)', response, flags=re.IGNORECASE)
if ddos_protection:
error = DDoSProtection
elif http_status_code in [408, 504]:
error = RequestTimeout
elif http_status_code in [401, 511]:
error = AuthenticationError
if error:
self.raise_error(error, url, method, exception if exception else http_status_code, details)
self.raise_error(error, url, method, exception if exception else http_status_code, response)

def handle_rest_response(self, response, url, method='GET', headers=None, body=None):
try:
Expand Down
2 changes: 1 addition & 1 deletion run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const testExchange = async (exchange) => {
const args = [exchange, ...symbol === 'all' ? [] : symbol]
, allTests = [

{ language: 'JavaScript', key: '--js', exec: ['node', 'js/test/test.js', ...args] },
{ language: 'JavaScript', key: '--js', exec: ['node', 'js/test/test.js', ...args] },
{ language: 'Python', key: '--python', exec: ['python', 'python/test/test.py', ...args] },
{ language: 'Python 2', key: '--python2', exec: ['python2', 'python/test/test.py', ...args] },
{ language: 'Python 3', key: '--python3', exec: ['python3', 'python/test/test_async.py', ...args] },
Expand Down

0 comments on commit 328adb8

Please sign in to comment.