aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2016-01-25 01:16:14 -0500
committerRapptz <[email protected]>2016-01-25 01:16:14 -0500
commit4d816c4ef3d2eaa5ffb970b0d1b187bc287f9c4c (patch)
treee80100f6139a87ccd322696f72f7c3172f498020
parent[commands] Fix discord.Invite special case handling in parameters. (diff)
downloaddiscord.py-4d816c4ef3d2eaa5ffb970b0d1b187bc287f9c4c.tar.xz
discord.py-4d816c4ef3d2eaa5ffb970b0d1b187bc287f9c4c.zip
HTTPException now has a text attribute if JSON is not available.
-rw-r--r--discord/errors.py7
-rw-r--r--discord/utils.py16
2 files changed, 17 insertions, 6 deletions
diff --git a/discord/errors.py b/discord/errors.py
index 3569aae0..62fff0b2 100644
--- a/discord/errors.py
+++ b/discord/errors.py
@@ -54,10 +54,15 @@ class HTTPException(DiscordException):
instance of `aiohttp.ClientResponse`__.
__ http://aiohttp.readthedocs.org/en/stable/client_reference.html#aiohttp.ClientResponse
+
+ .. attribute:: text
+
+ The text of the response if it wasn't JSON. Could be None.
"""
- def __init__(self, response, message=None):
+ def __init__(self, response, message=None, text=None):
self.response = response
+ self.text = text
fmt = '{0.reason} (status code: {0.status})'
if message:
diff --git a/discord/utils.py b/discord/utils.py
index 6ad98882..dc8c941d 100644
--- a/discord/utils.py
+++ b/discord/utils.py
@@ -172,13 +172,19 @@ def _verify_successful_response(response):
code = response.status
success = code >= 200 and code < 300
if not success:
- data = yield from response.json()
- message = data.get('message')
+ message = None
+ text = None
+ if response.headers['content-type'] == 'application/json':
+ data = yield from response.json()
+ message = data.get('message')
+ else:
+ text = yield from response.text()
+
if code == 403:
- raise Forbidden(response, message)
+ raise Forbidden(response, message, text)
elif code == 404:
- raise NotFound(response, message)
- raise HTTPException(response, message)
+ raise NotFound(response, message, text)
+ raise HTTPException(response, message, text)
def _get_mime_type_for_image(data):
if data.startswith(b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A'):