MCPcopy Create free account
hub / github.com/ccxt/ccxt / handle_errors

Method handle_errors

python/ccxt/async_support/binance.py:11638–11706  ·  view source on GitHub ↗
(self, code: int, reason: str, url: str, method: str, headers: dict, body: str, response, requestHeaders, requestBody)

Source from the content-addressed store, hash-verified

11636 return {}
11637
11638 def handle_errors(self, code: int, reason: str, url: str, method: str, headers: dict, body: str, response, requestHeaders, requestBody):
11639 if (code == 418) or (code == 429):
11640 raise DDoSProtection(self.id + ' ' + str(code) + ' ' + reason + ' ' + body)
11641 # error response in a form: {"code": -1013, "msg": "Invalid quantity."}
11642 # following block cointains legacy checks against message patterns in "msg" property
11643 # will switch "code" checks eventually, when we know all of them
11644 if code >= 400:
11645 if body.find('Price * QTY is zero or less') >= 0:
11646 raise InvalidOrder(self.id + ' order cost = amount * price is zero or less ' + body)
11647 if body.find('LOT_SIZE') >= 0:
11648 raise InvalidOrder(self.id + ' order amount should be evenly divisible by lot size ' + body)
11649 if body.find('PRICE_FILTER') >= 0:
11650 raise InvalidOrder(self.id + ' order price is invalid, i.e. exceeds allowed price precision, exceeds min price or max price limits or is invalid value in general, use self.price_to_precision(symbol, amount) ' + body)
11651 if response is None:
11652 return None # fallback to default error handler
11653 # response in format {'msg': 'The coin does not exist.', 'success': True/false}
11654 success = self.safe_bool(response, 'success', True)
11655 if not success:
11656 messageNew = self.safe_string(response, 'msg')
11657 parsedMessage = None
11658 if messageNew is not None:
11659 try:
11660 parsedMessage = json.loads(messageNew)
11661 except Exception as e:
11662 # do nothing
11663 parsedMessage = None
11664 if parsedMessage is not None:
11665 response = parsedMessage
11666 message = self.safe_string(response, 'msg')
11667 if message is not None:
11668 self.throw_exactly_matched_exception(self.get_exceptions_by_url(url, 'exact'), message, self.id + ' ' + message)
11669 self.throw_exactly_matched_exception(self.exceptions['exact'], message, self.id + ' ' + message)
11670 self.throw_broadly_matched_exception(self.get_exceptions_by_url(url, 'broad'), message, self.id + ' ' + message)
11671 self.throw_broadly_matched_exception(self.exceptions['broad'], message, self.id + ' ' + message)
11672 # checks against error codes
11673 error = self.safe_string(response, 'code')
11674 if error is not None:
11675 # https://github.com/ccxt/ccxt/issues/6501
11676 # https://github.com/ccxt/ccxt/issues/7742
11677 if (error == '200') or Precise.string_equals(error, '0'):
11678 return None
11679 # a workaround for {"code":-2015,"msg":"Invalid API-key, IP, or permissions for action."}
11680 # despite that their message is very confusing, it is raised by Binance
11681 # on a temporary ban, the API key is valid, but disabled for a while
11682 if (error == '-2015') and self.options['hasAlreadyAuthenticatedSuccessfully']:
11683 raise DDoSProtection(self.id + ' ' + body)
11684 feedback = self.id + ' ' + body
11685 if message == 'No need to change margin type.':
11686 # not an error
11687 # https://github.com/ccxt/ccxt/issues/11268
11688 # https://github.com/ccxt/ccxt/pull/11624
11689 # POST https://fapi.binance.com/fapi/v1/marginType 400 Bad Request
11690 # binanceusdm {"code":-4046,"msg":"No need to change margin type."}
11691 raise MarginModeAlreadySet(feedback)
11692 self.throw_exactly_matched_exception(self.get_exceptions_by_url(url, 'exact'), error, feedback)
11693 self.throw_exactly_matched_exception(self.exceptions['exact'], error, feedback)
11694 raise ExchangeError(feedback)
11695 if not success:

Callers 1

handle_ws_errorMethod · 0.95

Calls 12

get_exceptions_by_urlMethod · 0.95
DDoSProtectionClass · 0.90
InvalidOrderClass · 0.90
ExchangeErrorClass · 0.90
findMethod · 0.80
safe_boolMethod · 0.80
safe_stringMethod · 0.80
string_equalsMethod · 0.80
loadsMethod · 0.45

Tested by

no test coverage detected