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

handling api and http error exceptions proposal #51

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
29 changes: 22 additions & 7 deletions betfair/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,31 @@ def __init__(self, response, data):
super(AuthError, self).__init__(self.message)


class ApiError(BetfairError):
class ApiMetaError(BetfairError):

def __init__(self, response, data):
def __init__(self, response, message, details):
self.response = response
self.status_code = response.status_code
self.message = message
self.details = details
super(ApiMetaError, self).__init__(self.message)


class ApiError(ApiMetaError):

def __init__(self, response, data):
try:
error_data = data['error']['data']['APINGException']
self.message = error_data.get('errorCode', 'UNKNOWN')
self.details = error_data.get('errorDetails')
message = error_data.get('errorCode', 'UNKNOWN')
details = error_data.get('errorDetails')
except KeyError:
self.message = 'UNKNOWN'
self.details = None
super(ApiError, self).__init__(self.message)
message = 'UNKNOWN'
details = None
super(ApiError, self).__init__(response, message, details)


class ApiHttpError(ApiMetaError):

def __init__(self, response):
super(ApiHttpError, self).__init__(response, "error http return code: %s" %
response.status_code, None)
2 changes: 1 addition & 1 deletion betfair/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def check_status_code(response, codes=None):
else lambda resp: resp.status_code in codes
)
if not checker(response):
raise exceptions.ApiError(response, response.json())
raise exceptions.ApiHttpError(response)


def result_or_error(response):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_betfair.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ def test_login_error(client, login_failure):


def test_login_bad_code(client, login_bad_code):
with pytest.raises(exceptions.ApiError) as excinfo:
with pytest.raises(exceptions.ApiHttpError) as excinfo:
client.login('name', 'wrong')
error = excinfo.value
assert error.status_code == 422
assert error.message == 'UNKNOWN'
assert error.message == 'error http return code: 422'


def test_keepalive_success(logged_in_client, keepalive_success):
Expand Down