From 5f9c290869c39210e2cee14fb44f3e72da49ac90 Mon Sep 17 00:00:00 2001 From: DeepPython Date: Thu, 12 Oct 2023 00:55:08 +1100 Subject: [PATCH] Update exceptions.py In this improved version, messages are centrally located in the ERROR_MESSAGES dictionary, making future changes easier. The use of the message key in the specific exceptions allows for code to be less redundant and more unified in its structure. --- binance/exceptions.py | 60 +++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/binance/exceptions.py b/binance/exceptions.py index 9a3fd313..92b02067 100644 --- a/binance/exceptions.py +++ b/binance/exceptions.py @@ -1,15 +1,23 @@ # 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: {}" +} 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') @@ -17,72 +25,50 @@ def __init__(self, response, status_code, text): 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