Skip to content

Commit

Permalink
Adding separate exception for http error. Inherits from ApiMetaError
Browse files Browse the repository at this point in the history
which ApiError also inherits from.
  • Loading branch information
ms5 committed Jul 9, 2015
1 parent c6ab8ac commit 5269bc8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
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

0 comments on commit 5269bc8

Please sign in to comment.