Minor refactoring - Separate exception from the parsing logic. Expect Discord API to always return JSON payload with all keys on Ratelimit

This commit is contained in:
thec0sm0s
2020-10-07 17:07:52 +05:30
parent 17b7c6b82d
commit ad380cfb5c
3 changed files with 10 additions and 21 deletions

View File

@@ -1,6 +1,3 @@
import json
class HttpException(Exception):
"""Base Exception class representing a HTTP exception."""
@@ -12,8 +9,6 @@ class RateLimited(HttpException):
Attributes
----------
response : requests.Response
The actual response object received from Discord API.
json : dict
The actual JSON data received. Shorthand to ``response.json()``.
message : str
@@ -25,19 +20,13 @@ class RateLimited(HttpException):
"""
def __init__(self, response):
self.response = response
try:
self.json = self.response.json()
except json.JSONDecodeError:
self.json = dict()
self.message = self.response.text
else:
self.message = self.json["message"]
self.is_global = self.json["global"]
self.retry_after = self.json["retry_after"]
finally:
super().__init__(self.message)
def __init__(self, json, headers):
self.json = json
self.headers = headers
self.message = self.json["message"]
self.is_global = self.json["global"]
self.retry_after = self.json["retry_after"]
super().__init__(self.message)
class Unauthorized(HttpException):