-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
PJV0043
wants to merge
1
commit into
sammchardy:master
Choose a base branch
from
PJV0043:patch-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Update exceptions.py #1364
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {}" | ||
} | ||
|
||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.