Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[W.I.P.] send error message from response to tapioca exception. #124

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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