Skip to content

Commit 51e4403

Browse files
author
Shariq Hashme
authored
Merge pull request #9 from scaleapi/shariq
Improved error handling and exception print message
2 parents 47479db + a84c4dd commit 51e4403

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

scaleapi/__init__.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
class ScaleException(Exception):
2424
def __init__(self, message, errcode):
25-
super(ScaleException, self).__init__(message)
25+
super(ScaleException, self).__init__('<Response [{}]> {}'.format(errcode, message))
2626
self.code = errcode
2727

2828

@@ -57,7 +57,15 @@ def _getrequest(self, endpoint, params={}):
5757

5858
if r.status_code == 200:
5959
return r.json()
60-
raise ScaleException(r.json()['error'], r.status_code)
60+
else:
61+
try:
62+
error = r.json()['error']
63+
except ValueError:
64+
error = r.text
65+
if r.status_code == 400:
66+
raise ScaleInvalidRequest(error, r.status_code)
67+
else:
68+
raise ScaleException(error, r.status_code)
6169

6270
def _postrequest(self, endpoint, payload=None):
6371
"""Makes a post request to an endpoint.
@@ -73,9 +81,15 @@ def _postrequest(self, endpoint, payload=None):
7381

7482
if r.status_code == 200:
7583
return r.json()
76-
if r.status_code == 400:
77-
raise ScaleInvalidRequest(r.json()['error'], r.status_code)
78-
raise ScaleException(r.json()['error'], r.status_code)
84+
else:
85+
try:
86+
error = r.json()['error']
87+
except ValueError:
88+
error = r.text
89+
if r.status_code == 400:
90+
raise ScaleInvalidRequest(error, r.status_code)
91+
else:
92+
raise ScaleException(error, r.status_code)
7993

8094
def fetch_task(self, task_id):
8195
"""Fetches a task.

setup.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
setup(
2626
name = 'scaleapi',
2727
packages = ['scaleapi'],
28-
version = '0.2.13',
29-
description = 'The official Python client library for the Scale API, the API for human labor.',
28+
version = '0.2.14',
29+
description = 'The official Python client library for the Scale API, the API for human intelligence.',
3030
author = 'Calvin Huang',
3131
author_email = 'c@lvin.me',
3232
url = 'https://github.com/scaleapi/scaleapi-python-client',
33-
download_url = 'https://github.com/scaleapi/scaleapi-python-client/tarball/0.1.10',
3433
keywords = ['scale', 'scaleapi', 'humans', 'tasks', 'categorization', 'transcription', 'annotation', 'comparison', 'data collection', 'audio transcription'],
3534
install_requires = install_requires,
3635
classifiers = ['Programming Language :: Python :: 2.7',

0 commit comments

Comments
 (0)