Skip to content

Commit ed52f1a

Browse files
committed
Handle unicode decode errors as NetworkError
1 parent 6066a83 commit ed52f1a

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

tests/tests_enums.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import unittest
2+
3+
from tests.tests_tibiapy import TestCommons
4+
from tibiapy.enums import *
5+
6+
7+
class TestEnums(TestCommons, unittest.TestCase):
8+
def test_vocation_filter_from_name(self):
9+
"""Testing getting a VocationFilter entry from a vocation's name"""
10+
self.assertEqual(VocationFilter.KNIGHTS, VocationFilter.from_name("elite knight"))
11+
self.assertEqual(VocationFilter.KNIGHTS, VocationFilter.from_name("knight"))
12+
self.assertEqual(VocationFilter.KNIGHTS, VocationFilter.from_name("knights"))
13+
self.assertEqual(VocationFilter.ALL, VocationFilter.from_name("anything"))
14+
self.assertIsNone(VocationFilter.from_name("anything", all_fallback=False))

tibiapy/client.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
CACHE_LIMIT = 300
1919

2020

21-
class TibiaResponse:
21+
# This class is not used in this release.
22+
class _TibiaResponse: # pragma: no cover
2223
def __init__(self, data=None, cache_limit=CACHE_LIMIT, **kwargs):
2324
self.data = None
2425
self.cached = kwargs.get("cached")
@@ -59,7 +60,7 @@ def __init__(self, loop=None, session=None, *, proxy_url=None):
5960

6061
async def _initialize_session(self, proxy_url=None):
6162
headers = {
62-
'User-Agent': "Tibia.py/%s (+https://github.com/Galarzaa90/tibia.py" % tibiapy.__version__,
63+
'User-Agent': "Tibia.py/%s (+https://github.com/Galarzaa90/tibia.py)" % tibiapy.__version__,
6364
'Accept-Encoding': "deflate, gzip"
6465
}
6566
connector = aiohttp_socks.SocksConnector.from_url(proxy_url) if proxy_url else None
@@ -105,6 +106,8 @@ async def _get(self, url):
105106
raise NetworkError("aiohttp.ClientError: %s" % e, e)
106107
except aiohttp_socks.SocksConnectionError as e:
107108
raise NetworkError("aiohttp_socks.SocksConnectionError: %s" % e, e)
109+
except UnicodeDecodeError as e:
110+
raise NetworkError('UnicodeDecodeError: %s' % e, e)
108111

109112
async def _post(self, url, data):
110113
"""Base POST request, handling possible error statuses.
@@ -127,6 +130,10 @@ async def _post(self, url, data):
127130
return await resp.text()
128131
except aiohttp.ClientError as e:
129132
raise NetworkError("aiohttp.ClientError: %s" % e, e)
133+
except aiohttp_socks.SocksConnectionError as e:
134+
raise NetworkError("aiohttp_socks.SocksConnectionError: %s" % e, e)
135+
except UnicodeDecodeError as e:
136+
raise NetworkError('UnicodeDecodeError: %s' % e, e)
130137

131138
async def fetch_boosted_creature(self):
132139
"""Fetches today's boosted creature.
@@ -420,9 +427,9 @@ async def fetch_news_archive(self, begin_date, end_date, categories=None, types=
420427
if begin_date > end_date:
421428
raise ValueError("begin_date can't be more recent than end_date")
422429
if not categories:
423-
categories = NewsCategory.items()
430+
categories = list(NewsCategory)
424431
if not types:
425-
types = NewsType.items()
432+
types = list(NewsType)
426433
data = {
427434
"filter_begin_day": begin_date.day,
428435
"filter_begin_month": begin_date.month,

tibiapy/enums.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ def __str__(self):
2626
def __repr__(self):
2727
return "%s.%s" % (self.__class__.__name__, self.name)
2828

29-
@classmethod
30-
def items(cls):
31-
return [i for i in cls]
32-
3329

3430
class AccountStatus(BaseEnum):
3531
"""Possible account statuses."""

0 commit comments

Comments
 (0)