Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update exceptions.py #1364

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 23 additions & 37 deletions binance/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,88 +1,74 @@
# coding=utf-8
import json

ERROR_MESSAGES = {
"invalid_json": "Invalid JSON error message from Binance: {}",
"unknown_symbol": "Unknown symbol {}",
"inactive_symbol": "Attempting to trade an inactive symbol {}",
"min_amount": "Amount must be a multiple of {}",
"min_price": "Price must be at least {}",
"min_total": "Total must be at least {}",
"not_implemented": "Not implemented: {}"
}
Comment on lines +4 to +12

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, seems to me that you "could" decouple this. Comes to my mind that can create a "constant" file and specify in there all the constants (including those error messages) required by this service.

If you are not happy with this you are free to solve my comment / review.


class BinanceAPIException(Exception):

def __init__(self, response, status_code, text):
self.code = 0
try:
json_res = json.loads(text)
except ValueError:
self.message = 'Invalid JSON error message from Binance: {}'.format(response.text)
self.message = ERROR_MESSAGES["invalid_json"].format(response.text)
else:
self.code = json_res.get('code')
self.message = json_res.get('msg')
self.status_code = status_code
self.response = response
self.request = getattr(response, 'request', None)

def __str__(self): # pragma: no cover
return 'APIError(code=%s): %s' % (self.code, self.message)

def __str__(self):
return f'APIError(code={self.code}): {self.message}'

class BinanceRequestException(Exception):
def __init__(self, message):
self.message = message

def __str__(self):
return 'BinanceRequestException: %s' % self.message

return f'BinanceRequestException: {self.message}'

class BinanceOrderException(Exception):

def __init__(self, code, message):
def __init__(self, code, message_key, value=None):
self.code = code
self.message = message
self.message = ERROR_MESSAGES[message_key].format(value)

def __str__(self):
return 'BinanceOrderException(code=%s): %s' % (self.code, self.message)

return f'BinanceOrderException(code={self.code}): {self.message}'

class BinanceOrderMinAmountException(BinanceOrderException):

def __init__(self, value):
message = "Amount must be a multiple of %s" % value
super().__init__(-1013, message)

super().__init__(-1013, "min_amount", value)

class BinanceOrderMinPriceException(BinanceOrderException):

def __init__(self, value):
message = "Price must be at least %s" % value
super().__init__(-1013, message)

super().__init__(-1013, "min_price", value)

class BinanceOrderMinTotalException(BinanceOrderException):

def __init__(self, value):
message = "Total must be at least %s" % value
super().__init__(-1013, message)

super().__init__(-1013, "min_total", value)

class BinanceOrderUnknownSymbolException(BinanceOrderException):

def __init__(self, value):
message = "Unknown symbol %s" % value
super().__init__(-1013, message)

super().__init__(-1013, "unknown_symbol", value)

class BinanceOrderInactiveSymbolException(BinanceOrderException):

def __init__(self, value):
message = "Attempting to trade an inactive symbol %s" % value
super().__init__(-1013, message)

super().__init__(-1013, "inactive_symbol", value)

class BinanceWebsocketUnableToConnect(Exception):
pass


class NotImplementedException(Exception):
def __init__(self, value):
message = f'Not implemented: {value}'
super().__init__(message)

super().__init__(ERROR_MESSAGES["not_implemented"].format(value))

class UnknownDateFormat(Exception):
...
pass