From 3f83ee0a4ca9d843472f69cbb9a962504950470c Mon Sep 17 00:00:00 2001 From: codl Date: Mon, 11 Mar 2019 14:50:23 +0100 Subject: failing test for #160 --- tests/test_errors.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/test_errors.py diff --git a/tests/test_errors.py b/tests/test_errors.py new file mode 100644 index 0000000..77230c6 --- /dev/null +++ b/tests/test_errors.py @@ -0,0 +1,20 @@ +import pytest +from mastodon.Mastodon import MastodonAPIError +import re +try: + from mock import MagicMock +except ImportError: + from unittest.mock import MagicMock + +def test_nonstandard_errors(api): + response = MagicMock() + response.json = MagicMock(return_value= + "I am a non-standard instance and this error is a plain string.") + response.ok = False + session = MagicMock() + session.request = MagicMock(return_value=response) + + api.session = session + with pytest.raises(MastodonAPIError): + api.instance() + -- cgit v1.2.3 From 0666bdabf1c176e9074509f9cb398b4a83b33d2f Mon Sep 17 00:00:00 2001 From: codl Date: Mon, 11 Mar 2019 14:51:11 +0100 Subject: better handling of non-standard error responses, such as from pleroma --- mastodon/Mastodon.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index fc585ba..37e35b7 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -2214,9 +2214,12 @@ class Mastodon: if not response_object.ok: try: response = response_object.json(object_hook=self.__json_hooks) - if not isinstance(response, dict) or 'error' not in response: + if isinstance(response, dict) and 'error' in response: + error_msg = response['error'] + elif isinstance(response, str): + error_msg = response + else: error_msg = None - error_msg = response['error'] except ValueError: error_msg = None -- cgit v1.2.3 From e594bad9bb5c459862e7837b42b471d9e5937b6c Mon Sep 17 00:00:00 2001 From: codl Date: Mon, 15 Apr 2019 14:31:41 +0200 Subject: remove extraneous import --- tests/test_errors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_errors.py b/tests/test_errors.py index 77230c6..7329507 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -1,6 +1,6 @@ import pytest from mastodon.Mastodon import MastodonAPIError -import re + try: from mock import MagicMock except ImportError: -- cgit v1.2.3