def handle_errors(self, code, reason, url, method, headers, body): if code >= 400: if code == 418: raise DDoSProtection(self.id + ' ' + str(code) + ' ' + reason + ' ' + body) if body.find('MIN_NOTIONAL') >= 0: raise InvalidOrder(self.id + ' order cost = amount * price should be >(0.001 BTC or 0.01 ETH or 1 BNB or 1 USDT)' + body) if body.find('LOT_SIZE') >= 0: raise InvalidOrder(self.id + ' order amount should be evenly divisible by lot size, use self.amount_to_lots(symbol, amount) ' + body) if body.find('PRICE_FILTER') >= 0: raise InvalidOrder(self.id + ' order price exceeds allowed price precision or invalid, use self.price_to_precision(symbol, amount) ' + body) if body.find('Order does not exist') >= 0: raise OrderNotFound(self.id + ' ' + body) if body[0] == "{": response = json.loads(body) error = self.safe_value(response, 'code') if error is not None: if error == -2010: raise InsufficientFunds(self.id + ' ' + self.json(response)) elif error == -2011: raise OrderNotFound(self.id + ' ' + self.json(response)) elif error == -1013: # Invalid quantity raise InvalidOrder(self.id + ' ' + self.json(response)) elif error < 0: raise ExchangeError(self.id + ' ' + self.json(response))
def handle_errors(self, code, reason, url, method, headers, body): if not isinstance(body, basestring): return # fallback to default error handler if len(body) < 2: return # fallback to default error handler fixedJSONString = self.sanitize_broken_json_string(body) if fixedJSONString[0] == '{': response = json.loads(fixedJSONString) if 'Success' in response: success = self.safe_string(response, 'Success') if success == 'false': error = self.safe_string(response, 'Error') feedback = self.id if isinstance(error, basestring): feedback = feedback + ' ' + error if error.find('does not exist') >= 0: raise OrderNotFound(feedback) if error.find('Insufficient Funds') >= 0: raise InsufficientFunds(feedback) if error.find('Nonce has already been used') >= 0: raise InvalidNonce(feedback) else: feedback = feedback + ' ' + fixedJSONString raise ExchangeError(feedback)
def handle_errors(self, code, reason, url, method, headers, body): if code == 200: if body[0] != '{': # response is not JSON raise ExchangeError(self.id + ' returned a non-JSON reply: ' + body) response = json.loads(body) if 'success' in response: if not response['success']: error = self.safe_value(response, 'error') if not error: raise ExchangeError(self.id + ' returned a malformed error: ' + body) elif error == 'bad status': raise OrderNotFound(self.id + ' ' + error) elif error.find('It is not enough') >= 0: raise InsufficientFunds(self.id + ' ' + error) elif error == 'Requests too often': raise DDoSProtection(self.id + ' ' + error) elif error == 'not available': raise DDoSProtection(self.id + ' ' + error) elif error == 'external service unavailable': raise DDoSProtection(self.id + ' ' + error) # that's what fetchOpenOrders return if no open orders(fix for #489) elif error != 'no orders': raise ExchangeError(self.id + ' ' + error)
def handle_errors(self, code, reason, url, method, headers, body): response = None if code == 200 or code == 404 or code == 422: if (body[0] == '{') or (body[0] == '['): response = json.loads(body) else: # if not a JSON response raise ExchangeError(self.id + ' returned a non-JSON reply: ' + body) if code == 404: if 'message' in response: if response['message'] == 'Order not found': raise OrderNotFound(self.id + ' ' + body) elif code == 422: if 'errors' in response: errors = response['errors'] if 'user' in errors: messages = errors['user'] if messages.find('not_enough_free_balance') >= 0: raise InsufficientFunds(self.id + ' ' + body) elif 'quantity' in errors: messages = errors['quantity'] if messages.find('less_than_order_size') >= 0: raise InvalidOrder(self.id + ' ' + body)
def handle_errors(self, code, reason, url, method, headers, body): if code == 200: if body[0] != '{': # response is not JSON -> resort to default error handler return response = json.loads(body) if 'success' in response: if not response['success']: error = self.safe_string(response, 'error') if not error: raise ExchangeError(self.id + ' returned a malformed error: ' + body) if error == 'no orders': # returned by fetchOpenOrders if no open orders(fix for #489) -> not an error return feedback = self.id + ' ' + self.json(response) messages = self.exceptions['messages'] if error in messages: raise messages[error](feedback) if error.find('It is not enough') >= 0: raise InsufficientFunds(feedback) else: raise ExchangeError(feedback)
def handle_errors(self, code, reason, url, method, headers, body): if (code == 418) or (code == 429): raise DDoSProtection(self.id + ' ' + str(code) + ' ' + reason + ' ' + body) # error response in a form: {"code": -1013, "msg": "Invalid quantity."} # following block cointains legacy checks against message patterns in "msg" property # will switch "code" checks eventually, when we know all of them if code >= 400: if body.find('Price * QTY is zero or less') >= 0: raise InvalidOrder( self.id + ' order cost = amount * price is zero or less ' + body) if body.find('LOT_SIZE') >= 0: raise InvalidOrder( self.id + ' order amount should be evenly divisible by lot size, use self.amount_to_lots(symbol, amount) ' + body) if body.find('PRICE_FILTER') >= 0: raise InvalidOrder( self.id + ' order price exceeds allowed price precision or invalid, use self.price_to_precision(symbol, amount) ' + body) if len(body) > 0: if body[0] == '{': response = json.loads(body) # check success value for wapi endpoints # response in format {'msg': 'The coin does not exist.', 'success': True/false} success = self.safe_value(response, 'success', True) if not success: if 'msg' in response: try: response = json.loads(response['msg']) except Exception as e: response = {} # checks against error codes error = self.safe_string(response, 'code') if error is not None: exceptions = self.exceptions if error in exceptions: # a workaround for {"code":-2015,"msg":"Invalid API-key, IP, or permissions for action."} # despite that their message is very confusing, it is raised by Binance # on a temporary ban(the API key is valid, but disabled for a while) if (error == '-2015') and self.options[ 'hasAlreadyAuthenticatedSuccessfully']: raise DDoSProtection(self.id + ' temporary banned: ' + body) message = self.safe_string(response, 'msg') if message == 'Order would trigger immediately.': raise InvalidOrder(self.id + ' ' + body) elif message == 'Account has insufficient balance for requested action.': raise InsufficientFunds(self.id + ' ' + body) elif message == 'Rest API trading is not enabled.': raise InsufficientFunds(self.id + ' ' + body) raise exceptions[error](self.id + ' ' + body) else: raise ExchangeError(self.id + ': unknown error code: ' + body + ' ' + error) if not success: raise ExchangeError(self.id + ': success value False: ' + body)
def handle_errors(self, httpCode, reason, url, method, headers, body, response=None): if not isinstance(body, basestring): return # fallback to default error handler if len(body) < 2: return # fallback to default error handler if (body[0] == '{') or (body[0] == '['): response = json.loads(body) if 'code' in response: # # {"code": "100005", "msg": "request sign illegal", "data": null} # code = self.safe_string(response, 'code') if code is not None: message = self.safe_string(response, 'msg') feedback = self.id + ' ' + self.json(response) if code != '0': exceptions = self.exceptions if code in exceptions: if code == '1': # # {"code":"1","msg":"系统错误","data":null} # {“code”:“1",“msg”:“Balance insufficient,余额不足“,”data”:null} # if message.find('Balance insufficient') >= 0: raise InsufficientFunds(feedback) elif code == '2': if message == 'offsetNot Null': raise ExchangeError(feedback) elif message == 'api_keyNot EXIST': raise AuthenticationError(feedback) elif message == 'price precision exceed the limit': raise InvalidOrder(feedback) elif message == 'Parameter error': raise BadRequest(feedback) raise exceptions[code](feedback) else: raise ExchangeError(self.id + ' unknown "error" value: ' + self.json(response)) else: # # Google Translate: # 订单状态不能取消,订单取消失败 = Order status cannot be canceled # 根据订单号没有查询到订单,订单取消失败 = The order was not queried according to the order number # # {"code":"0","msg":"suc","data":{"success":[],"failed":[{"err-msg":"订单状态不能取消,订单取消失败","order-id":32857051,"err-code":"8"}]}} # {"code":"0","msg":"suc","data":{"success":[],"failed":[{"err-msg":"Parameter error","order-id":32857050,"err-code":"2"},{"err-msg":"订单状态不能取消,订单取消失败","order-id":32857050,"err-code":"8"}]}} # {"code":"0","msg":"suc","data":{"success":[],"failed":[{"err-msg":"Parameter error","order-id":98549677,"err-code":"2"},{"err-msg":"根据订单号没有查询到订单,订单取消失败","order-id":98549677,"err-code":"8"}]}} # if feedback.find('订单状态不能取消,订单取消失败') >= 0: if feedback.find('Parameter error') >= 0: raise OrderNotFound(feedback) else: raise InvalidOrder(feedback) elif feedback.find('根据订单号没有查询到订单,订单取消失败') >= 0: raise OrderNotFound(feedback)
def handle_errors(self, code, reason, url, method, headers, body): if body.find('Invalid nonce') >= 0: raise InvalidNonce(self.id + ' ' + body) if body.find('Insufficient funds') >= 0: raise InsufficientFunds(self.id + ' ' + body)
def change_used(self, change): change = _convert_float(change) new_value = self._used + change if _BALANCE_CHECK and new_value > self._total: raise InsufficientFunds('Balance too little') self._used = new_value
def change_total(self, change): change = _convert_float(change) new_value = self._total + change if _BALANCE_CHECK and new_value < Decimal('0'): raise InsufficientFunds('Balance too little') self._total = new_value