Merge pull request #162 from codl/160

better handling of non-standard error responses, such as from pleroma
This commit is contained in:
Lorenz Diener 2019-04-27 17:17:08 +02:00 cometido por GitHub
commit 450ebd983f
No se encontró ninguna clave conocida en la base de datos para esta firma
ID de clave GPG: 4AEE18F83AFDEB23
S'han modificat 2 arxius amb 25 adicions i 2 eliminacions

Veure arxiu

@ -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

20
tests/test_errors.py Normal file
Veure arxiu

@ -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()