Skip to content

Commit

Permalink
feature(adapters.py): add get_error_message function for exception ha…
Browse files Browse the repository at this point in the history
…ndling
  • Loading branch information
alexsavio committed Jun 4, 2018
1 parent 078e72a commit e898655
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
11 changes: 11 additions & 0 deletions tapioca/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def get_request_kwargs(self, api_params, *args, **kwargs):
})
return kwargs

def get_error_message(self, data, response=None):
return str(data)

def process_response(self, response):
if 500 <= response.status_code < 600:
raise ResponseProcessException(ServerError, None)
Expand Down Expand Up @@ -118,6 +121,14 @@ def response_to_native(self, response):
if response.content.strip():
return response.json()

def get_error_message(self, data, response=None):
if not data and response.content.strip():
data = json.loads(response.content)

if data and 'error' in data:
return data.get('error', '')



class XMLAdapterMixin(object):

Expand Down
6 changes: 5 additions & 1 deletion tapioca/tapioca.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ def _make_request(self, request_method, refresh_token=None, *args, **kwargs):
except ResponseProcessException as e:
client = self._wrap_in_tapioca(e.data, response=response,
request_kwargs=request_kwargs)
tapioca_exception = e.tapioca_exception(client=client)

error_message = self._api.get_error_message(data=e.data,
response=response)
tapioca_exception = e.tapioca_exception(message=error_message,
client=client)

should_refresh_token = (refresh_token is not False and
self._refresh_token_default)
Expand Down
25 changes: 22 additions & 3 deletions tests/test_tapioca.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

import xmltodict
from collections import OrderedDict
from decimal import Decimal

from tapioca.tapioca import TapiocaClient
from tapioca.exceptions import ClientError
from tapioca.exceptions import ClientError, ServerError

from tests.client import TesterClient, TokenRefreshClient, XMLClient, FailTokenRefreshClient

Expand Down Expand Up @@ -308,6 +307,26 @@ def test_carries_request_kwargs_over_calls(self):
self.assertIn('data', request_kwargs)
self.assertIn('headers', request_kwargs)

@responses.activate
def test_thrown_tapioca_exception_with_clienterror_data(self):
responses.add(responses.GET, self.wrapper.test().data,
body='{"error": "bad request test"}',
status=400,
content_type='application/json')
with self.assertRaises(ClientError) as client_exception:
self.wrapper.test().get()
self.assertIn("bad request test", client_exception.exception.args)

@responses.activate
def test_thrown_tapioca_exception_with_servererror_data(self):
responses.add(responses.GET, self.wrapper.test().data,
body='{"error": "server error test"}',
status=500,
content_type='application/json')
with self.assertRaises(ServerError) as server_exception:
self.wrapper.test().get()
self.assertIn("server error test", server_exception.exception.args)


class TestIteratorFeatures(unittest.TestCase):

Expand Down Expand Up @@ -475,7 +494,7 @@ def test_token_expired_automatically_refresh_authentication(self):
def request_callback(request):
if self.first_call:
self.first_call = False
return (401, {'content_type': 'application/json'}, json.dumps('{"error": "Token expired"}'))
return (401, {'content_type': 'application/json'}, json.dumps({"error": "Token expired"}))
else:
self.first_call = None
return (201, {'content_type': 'application/json'}, '')
Expand Down

0 comments on commit e898655

Please sign in to comment.