Skip to content

Commit

Permalink
handle "No JSON object could be decoded" exception
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteca committed Jun 15, 2016
1 parent 23f15c2 commit 23df674
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 10 additions & 1 deletion monkeylearn/utils.py
Expand Up @@ -20,6 +20,7 @@ def __init__(self, result, responses):

class SleepRequestsMixin(object):
def make_request(self, url, method, data=None, sleep_if_throttled=True):
failure_counter = 0
while True:
if data:
response = requests.request(
Expand All @@ -41,7 +42,15 @@ def make_request(self, url, method, data=None, sleep_if_throttled=True):
}
)

response_json = response.json()
try:
response_json = response.json()
except ValueError: # No JSON object could be decoded
failure_counter += 1
if failure_counter > 3:
raise
time.sleep(2 * failure_counter)
continue
failure_counter = 0
if sleep_if_throttled and response.status_code == 429 and 'seconds' in response_json['detail']:
seconds = re.findall(r'available in (\d+) seconds', response_json['detail'])[0]
time.sleep(int(seconds))
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -4,12 +4,12 @@

setup(
name='monkeylearn',
version='0.3.1',
version='0.3.2',
author='MonkeyLearn',
author_email='hello@monkeylearn.com',
description='Official Python client for the MonkeyLearn API',
url='https://github.com/monkeylearn/monkeylearn-python',
download_url='https://github.com/monkeylearn/monkeylearn-python/tarball/v0.3.1',
download_url='https://github.com/monkeylearn/monkeylearn-python/tarball/v0.3.2',
keywords=['monkeylearn', 'machine learning', 'python'],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down

1 comment on commit 23df674

@brusteca
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue isn't fixed

Please sign in to comment.