From a8fef798106292acb1f5a65ccfbf5b35358fa622 Mon Sep 17 00:00:00 2001 From: Tarsis Azevedo Date: Tue, 25 Oct 2016 11:34:38 -0200 Subject: [PATCH] tapioca/tapioca: send error message from response to exception. Related to #95 --- tapioca/tapioca.py | 7 ++++++- tests/test_tapioca.py | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/tapioca/tapioca.py b/tapioca/tapioca.py index c4849a1..b604de6 100755 --- a/tapioca/tapioca.py +++ b/tapioca/tapioca.py @@ -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) diff --git a/tests/test_tapioca.py b/tests/test_tapioca.py index 5261fa7..6f7bf35 100755 --- a/tests/test_tapioca.py +++ b/tests/test_tapioca.py @@ -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 @@ -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):