Skip to content

Commit

Permalink
tapioca/tapioca: send error message from response to exception. Relat…
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsisazevedo committed Oct 25, 2016
1 parent 0e85d21 commit a8fef79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion tapioca/tapioca.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,12 @@ 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)
message = ""
if e.data:
if type(e.data) == str:
e.data = json.loads(e.data)
message = e.data.get("error", "")
tapioca_exception = e.tapioca_exception(client=client, message=message)

should_refresh_token = (refresh_token is not False and
self._refresh_token_default)
Expand Down
19 changes: 18 additions & 1 deletion tests/test_tapioca.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json

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

from tests.client import TesterClient, TokenRefreshClient

Expand Down Expand Up @@ -304,6 +304,23 @@ 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_error_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.add(responses.GET, self.wrapper.test().data,
body='{"error": "server error test"}',
status=500,
content_type='application/json')
with self.assertRaises(ServerError) as client_exception:
self.wrapper.test().get()
self.assertIn("server error test", client_exception.exception.args)


class TestIteratorFeatures(unittest.TestCase):

Expand Down

0 comments on commit a8fef79

Please sign in to comment.