EnkaNetwork.py
EnkaNetwork.py copied to clipboard
Add Base Class for Handling EnkaNetwork Errors
Our hope is to create a base class that can gracefully catch exceptions thrown from the EnkaNetwork.py module
class EnkaNetworError(Exception):
"""Base class for EnkaNetwork errors."""
class NetworkError(EnkaNetworError):
"""Base class for exceptions due to networking errors."""
class TimedOut(NetworkError):
"""Raised when a request took too long to finish."""
class BadRequest(EnkaNetworError):
"""Raised when EnkaNetwork could not process the request correctly."""
class EnkaValidateFailed(BadRequest):
"""Exception that's raised for when status code 400 occurs."""
...
NetworkError
can be from the aiohttp
base exception.
try:
await do_request()
except asyncio.TimeoutError as err:
# aiohttp.ClientSession throws subclasses of aiohttp.ClientError on connection issues,
# except for timeouts,
# where it throws asyncio.TimeoutError.
raise TimedOut from err
except aiohttp.ClientError as err:
raise NetworkError from err
We include the class name for easier debugging. Especially useful if the error.
If the above things are done, developers can better handle exceptions.
client = EnkaNetworkAPI(lang="en")
try:
# do something, like get uid
async with client:
data = await client.fetch_user(uid)
except TimedOut:
print("Connection timeout, please try again")
except BadRequest as exc:
print(exc.message)
# Notify user information that EnkaNetwork could not process the request correctly
except EnkaNetworError as exc:
sentry.report(exc)
# Notify developer information that something wrong with EnkaNetwor.py modules
Avoid catching very general exceptions. (PYL-W0703)