Skip to content

Commit 784e6cb

Browse files
author
Craig Christenson
committed
handle exceptions better
do not force mode attr on credentials
1 parent ad185ae commit 784e6cb

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

twocheckout/api_request.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ class Api:
1717
def credentials(cls, credentials):
1818
Api.username = credentials['username']
1919
Api.password = credentials['password']
20+
if 'mode' in credentials:
21+
Api.mode = credentials['mode']
2022

2123
@classmethod
2224
def auth_credentials(cls, credentials):
2325
Api.private_key = credentials['private_key']
2426
Api.seller_id = credentials['seller_id']
25-
Api.mode = credentials['mode']
27+
if 'mode' in credentials:
28+
Api.mode = credentials['mode']
2629

2730
@classmethod
2831
def call(cls, method, params=None):
@@ -34,11 +37,14 @@ def call(cls, method, params=None):
3437
result = urllib2.urlopen(req).read()
3538
return json.loads(result)
3639
except urllib2.HTTPError, e:
37-
exception = json.loads(e.read())
38-
if method == 'authService':
39-
raise TwocheckoutError(exception['exception']['errorCode'], exception['exception']['errorMsg'])
40+
if not hasattr(e, 'read'):
41+
raise TwocheckoutError(e.code, e.msg)
4042
else:
41-
raise TwocheckoutError(exception['errors'][0]['code'], exception['errors'][0]['message'])
43+
exception = json.loads(e.read())
44+
if method == 'authService':
45+
raise TwocheckoutError(exception['exception']['errorCode'], exception['exception']['errorMsg'])
46+
else:
47+
raise TwocheckoutError(exception['errors'][0]['code'], exception['errors'][0]['message'])
4248

4349
@classmethod
4450
def set_opts(cls, method, params=None):

0 commit comments

Comments
 (0)