Merge pull request #162 from codl/160
better handling of non-standard error responses, such as from pleroma
This commit is contained in:
commit
450ebd983f
S'han modificat 2 arxius amb 25 adicions i 2 eliminacions
|
@ -2214,9 +2214,12 @@ class Mastodon:
|
||||||
if not response_object.ok:
|
if not response_object.ok:
|
||||||
try:
|
try:
|
||||||
response = response_object.json(object_hook=self.__json_hooks)
|
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 = None
|
|
||||||
error_msg = response['error']
|
error_msg = response['error']
|
||||||
|
elif isinstance(response, str):
|
||||||
|
error_msg = response
|
||||||
|
else:
|
||||||
|
error_msg = None
|
||||||
except ValueError:
|
except ValueError:
|
||||||
error_msg = None
|
error_msg = None
|
||||||
|
|
||||||
|
|
20
tests/test_errors.py
Normal file
20
tests/test_errors.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import pytest
|
||||||
|
from mastodon.Mastodon import MastodonAPIError
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
Loading…
Referencia en una nova incidència