Support for API v7 error handling.
This commit is contained in:
parent
22150b20b6
commit
3983eb3ba5
@ -52,28 +52,58 @@ class GatewayNotFound(DiscordException):
|
|||||||
message = 'The gateway to connect to discord was not found.'
|
message = 'The gateway to connect to discord was not found.'
|
||||||
super(GatewayNotFound, self).__init__(message)
|
super(GatewayNotFound, self).__init__(message)
|
||||||
|
|
||||||
|
def flatten_error_dict(d, key=''):
|
||||||
|
items = []
|
||||||
|
for k, v in d.items():
|
||||||
|
new_key = key + '.' + k if key else k
|
||||||
|
|
||||||
|
if isinstance(v, dict):
|
||||||
|
try:
|
||||||
|
_errors = v['_errors']
|
||||||
|
except Exception:
|
||||||
|
items.extend(flatten_error_dict(v, new_key).items())
|
||||||
|
else:
|
||||||
|
items.append((new_key, ' '.join(x.get('message', '') for x in _errors)))
|
||||||
|
else:
|
||||||
|
items.append((new_key, v))
|
||||||
|
|
||||||
|
return dict(items)
|
||||||
|
|
||||||
class HTTPException(DiscordException):
|
class HTTPException(DiscordException):
|
||||||
"""Exception that's thrown when an HTTP request operation fails.
|
"""Exception that's thrown when an HTTP request operation fails.
|
||||||
|
|
||||||
.. attribute:: response
|
Attributes
|
||||||
|
------------
|
||||||
|
response: aiohttp.ClientResponse
|
||||||
The response of the failed HTTP request. This is an
|
The response of the failed HTTP request. This is an
|
||||||
instance of `aiohttp.ClientResponse`__.
|
instance of `aiohttp.ClientResponse`__.
|
||||||
|
|
||||||
__ http://aiohttp.readthedocs.org/en/stable/client_reference.html#aiohttp.ClientResponse
|
__ http://aiohttp.readthedocs.org/en/stable/client_reference.html#aiohttp.ClientResponse
|
||||||
|
|
||||||
.. attribute:: text
|
text: str
|
||||||
|
|
||||||
The text of the error. Could be an empty string.
|
The text of the error. Could be an empty string.
|
||||||
|
status: int
|
||||||
|
The status code of the HTTP request.
|
||||||
|
code: int
|
||||||
|
The Discord specific error code for the failure.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, response, message):
|
def __init__(self, response, message):
|
||||||
self.response = response
|
self.response = response
|
||||||
if type(message) is dict:
|
self.status = response.status
|
||||||
self.text = message.get('message', '')
|
if isinstance(message, dict):
|
||||||
self.code = message.get('code', 0)
|
self.code = message.get('code', 0)
|
||||||
|
base = message.get('message', '')
|
||||||
|
errors = message.get('errors')
|
||||||
|
if errors:
|
||||||
|
errors = flatten_error_dict(errors)
|
||||||
|
helpful = '\n'.join('In %s: %s' % t for t in errors.items())
|
||||||
|
self.text = base + '\n' + helpful
|
||||||
|
else:
|
||||||
|
self.text = base
|
||||||
else:
|
else:
|
||||||
self.text = message
|
self.text = message
|
||||||
|
self.code = 0
|
||||||
|
|
||||||
fmt = '{0.reason} (status code: {0.status})'
|
fmt = '{0.reason} (status code: {0.status})'
|
||||||
if len(self.text):
|
if len(self.text):
|
||||||
|
@ -46,7 +46,7 @@ def json_or_text(response):
|
|||||||
return text
|
return text
|
||||||
|
|
||||||
class Route:
|
class Route:
|
||||||
BASE = 'https://discordapp.com/api/v6'
|
BASE = 'https://discordapp.com/api/v7'
|
||||||
|
|
||||||
def __init__(self, method, path, **parameters):
|
def __init__(self, method, path, **parameters):
|
||||||
self.path = path
|
self.path = path
|
||||||
|
Loading…
x
Reference in New Issue
Block a user