Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HTTPException message #59

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions discohook/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ def __init__(self, message: str):
class HTTPException(Exception):
"""Raised when an HTTP request operation fails."""

def __init__(self, resp: aiohttp.ClientResponse, data: Any):
def __init__(self, resp: aiohttp.ClientResponse, message: str):
self.resp = resp
message = f"[{resp.method}] {resp.url.path} {resp.status} with code({data['code']}): {data['message']}"
super().__init__(message)

@classmethod
async def create(cls, resp: aiohttp.ClientResponse):
if resp.headers.get("content-type") == "application/json":
data = await resp.json()
else:
data = await resp.text()
message = f"[{resp.status} {resp.method}] {resp.url.path}\n{data}"
return cls(resp, message)
2 changes: 1 addition & 1 deletion discohook/https.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def request(
json=json,
)
if resp.status >= 400:
raise HTTPException(resp, await resp.json())
raise await HTTPException.create(resp)
return resp

async def fetch_application(self):
Expand Down