Skip to content

Commit

Permalink
Merge pull request quandl#70 from quandl/AP-1808/handle-bad-error-res…
Browse files Browse the repository at this point in the history
…ponse

handle non-json non quandl error responses
  • Loading branch information
ccleung committed May 30, 2016
2 parents 4681d4e + 41c2273 commit e2540dc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 3.0.1 - 2016-05-25

* Handle unexpected errors

### 3.0.0 - 2016-04-22

* Datatables for developers
Expand Down
10 changes: 8 additions & 2 deletions quandl/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@ def execute_request(cls, http_verb, url, **options):
def parse(cls, response):
try:
return response.json()
except ValueError as e:
raise QuandlError(str(e), response.status_code, response.text)
except ValueError:
raise QuandlError(http_status=response.status_code, http_body=response.text)

@classmethod
def handle_api_error(cls, resp):
error_body = cls.parse(resp)

# if our app does not form a proper quandl_error response
# throw generic error
if 'quandl_error' not in error_body:
raise QuandlError(http_status=resp.status_code, http_body=resp.text)

code = error_body['quandl_error']['code']
message = error_body['quandl_error']['message']
prog = re.compile('^QE([a-zA-Z])x')
Expand Down
8 changes: 6 additions & 2 deletions quandl/errors/quandl_error.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
class QuandlError(RuntimeError):
def __init__(self, quandl_message, http_status=None, http_body=None, http_headers=None,
GENERIC_ERROR_MESSAGE = 'Something went wrong. Please try again. \
If you continue to have problems, please contact us at [email protected].'

def __init__(self, quandl_message=None, http_status=None, http_body=None, http_headers=None,
quandl_error_code=None, response_data=None):
self.http_status = http_status
self.http_body = http_body
self.http_headers = http_headers if http_headers is not None else {}

self.quandl_error_code = quandl_error_code
self.quandl_message = quandl_message
self.quandl_message = quandl_message if quandl_message is not None \
else self.GENERIC_ERROR_MESSAGE
self.response_data = response_data

def __str__(self):
Expand Down
2 changes: 1 addition & 1 deletion quandl/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '3.0.0'
VERSION = '3.0.1'
12 changes: 11 additions & 1 deletion test/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ def test_parse_error(self):
self.assertRaises(
QuandlError, lambda: Connection.request('get', 'databases'))

@httpretty.activate
def test_non_quandl_error(self):
httpretty.register_uri(httpretty.GET,
"https://www.quandl.com/api/v3/databases",
body=json.dumps(
{'foobar':
{'code': 'blah', 'message': 'something went wrong'}}), status=500)
self.assertRaises(
QuandlError, lambda: Connection.request('get', 'databases'))

@httpretty.activate
@patch('quandl.connection.Connection.execute_request')
def test_build_request(self, mock):
Expand All @@ -57,6 +67,6 @@ def test_build_request(self, mock):
'accept': ('application/json, '
'application/vnd.quandl+json;version=2015-04-09'),
'request-source': 'python',
'request-source-version': '3.0.0'},
'request-source-version': '3.0.1'},
params={'per_page': 10, 'page': 2})
self.assertEqual(mock.call_args, expected)

0 comments on commit e2540dc

Please sign in to comment.