From 7de76c67d00ead5560dd81738224bf2a8a9bfc40 Mon Sep 17 00:00:00 2001 From: imp <106537315+imptype@users.noreply.github.com> Date: Wed, 24 Jul 2024 20:46:25 +0100 Subject: [PATCH 1/4] check content type for httpexception --- discohook/https.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/discohook/https.py b/discohook/https.py index ba89aba..ac2b997 100644 --- a/discohook/https.py +++ b/discohook/https.py @@ -51,7 +51,11 @@ async def request( json=json, ) if resp.status >= 400: - raise HTTPException(resp, await resp.json()) + if resp.headers.get("content-type") == "application/json": + text = await resp.json() + else: + text = await resp.text() + raise HTTPException(resp, text) return resp async def fetch_application(self): From 21f10dc980e6b1d68fabac7370824aad3b53acd2 Mon Sep 17 00:00:00 2001 From: imp <106537315+imptype@users.noreply.github.com> Date: Wed, 24 Jul 2024 21:19:49 +0100 Subject: [PATCH 2/4] be more explicit with httpexception --- discohook/errors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discohook/errors.py b/discohook/errors.py index 1bfd55f..b95fe34 100644 --- a/discohook/errors.py +++ b/discohook/errors.py @@ -32,5 +32,5 @@ class HTTPException(Exception): def __init__(self, resp: aiohttp.ClientResponse, data: Any): self.resp = resp - message = f"[{resp.method}] {resp.url.path} {resp.status} with code({data['code']}): {data['message']}" + message = f"[{resp.status} {resp.method}] {resp.url.path}\n{data}" super().__init__(message) From 6b5af7ee4cbdc26fac443bc5a53a4d7017580c12 Mon Sep 17 00:00:00 2001 From: imp <106537315+imptype@users.noreply.github.com> Date: Wed, 5 Feb 2025 20:07:48 +0000 Subject: [PATCH 3/4] move to httpexception ainit --- discohook/errors.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/discohook/errors.py b/discohook/errors.py index b95fe34..cc33a4c 100644 --- a/discohook/errors.py +++ b/discohook/errors.py @@ -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.status} {resp.method}] {resp.url.path}\n{data}" 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) From ec017fbf19a09a3966947e0597f3cf2b5310438e Mon Sep 17 00:00:00 2001 From: imp <106537315+imptype@users.noreply.github.com> Date: Wed, 5 Feb 2025 20:09:10 +0000 Subject: [PATCH 4/4] use async HTTPException.create() --- discohook/https.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/discohook/https.py b/discohook/https.py index ac2b997..d207720 100644 --- a/discohook/https.py +++ b/discohook/https.py @@ -51,11 +51,7 @@ async def request( json=json, ) if resp.status >= 400: - if resp.headers.get("content-type") == "application/json": - text = await resp.json() - else: - text = await resp.text() - raise HTTPException(resp, text) + raise await HTTPException.create(resp) return resp async def fetch_application(self):