Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Handle both JSONDecodeError and ValueError due to differences in json vs. simplejson #1645

Merged
merged 4 commits into from Aug 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions boto/cloudsearch/search.py
Expand Up @@ -37,7 +37,6 @@ class CommitMismatchError(Exception):


class SearchResults(object):

def __init__(self, **attrs):
self.rid = attrs['info']['rid']
# self.doc_coverage_pct = attrs['info']['doc-coverage-pct']
Expand Down Expand Up @@ -291,7 +290,7 @@ def __call__(self, query):
r = requests.get(url, params=params)
try:
data = json.loads(r.content)
except json.JSONDecodeError,e:
except ValueError, e:
if r.status_code == 403:
msg = ''
import re
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/cloudsearch/test_exceptions.py
@@ -0,0 +1,37 @@
import mock
from boto.compat import json
from tests.unit import unittest

from .test_search import HOSTNAME, CloudSearchSearchBaseTest
from boto.cloudsearch.search import SearchConnection, SearchServiceException


def fake_loads_value_error(content, *args, **kwargs):
"""Callable to generate a fake ValueError"""
raise ValueError("HAHAHA! Totally not simplejson & you gave me bad JSON.")


def fake_loads_json_error(content, *args, **kwargs):
"""Callable to generate a fake JSONDecodeError"""
raise json.JSONDecodeError('Using simplejson & you gave me bad JSON.',
'', 0)


class CloudSearchJSONExceptionTest(CloudSearchSearchBaseTest):
response = '{}'

def test_no_simplejson_value_error(self):
with mock.patch.object(json, 'loads', fake_loads_value_error):
search = SearchConnection(endpoint=HOSTNAME)

with self.assertRaisesRegexp(SearchServiceException, 'non-json'):
search.search(q='test')

@unittest.skipUnless(hasattr(json, 'JSONDecodeError'),
'requires simplejson')
def test_simplejson_jsondecodeerror(self):
with mock.patch.object(json, 'loads', fake_loads_json_error):
search = SearchConnection(endpoint=HOSTNAME)

with self.assertRaisesRegexp(SearchServiceException, 'non-json'):
search.search(q='test')