From b639fdae22d392904263237e07395e2c909f28b4 Mon Sep 17 00:00:00 2001 From: Jordan Stewart Date: Wed, 6 Mar 2024 22:06:01 +1100 Subject: [PATCH 1/2] raise exception instead of log an error --- discord_webhook/webhook.py | 4 ++-- discord_webhook/webhook_exceptions.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/discord_webhook/webhook.py b/discord_webhook/webhook.py index 7b1ff06..32ee471 100644 --- a/discord_webhook/webhook.py +++ b/discord_webhook/webhook.py @@ -7,7 +7,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union import requests -from .webhook_exceptions import ColorNotInRangeException +from .webhook_exceptions import ColorNotInRangeException, DiscordException logger = logging.getLogger(__name__) @@ -448,7 +448,7 @@ def execute(self, remove_embeds: bool = False) -> "requests.Response": response = self.handle_rate_limit(response, self.api_post_request) logger.debug("Webhook executed") else: - logger.error( + raise DiscordException( "Webhook status code {status_code}: {content}".format( status_code=response.status_code, content=response.content.decode("utf-8"), diff --git a/discord_webhook/webhook_exceptions.py b/discord_webhook/webhook_exceptions.py index 6342cf1..e5b5dde 100644 --- a/discord_webhook/webhook_exceptions.py +++ b/discord_webhook/webhook_exceptions.py @@ -16,3 +16,12 @@ def __init__(self, color: Union[str, int], message=None) -> None: " (HEXADECIMAL)." ) super().__init__(message) + +class DiscordException(Exception): + """ + This Exception is throw as a generic error from discord's API. + """ + + def __init__(self, *args: object) -> None: + super().__init__(*args) + From 18dfa635de54e690f94b80625b99df21157c0dc7 Mon Sep 17 00:00:00 2001 From: jordanst3wart Date: Wed, 1 May 2024 21:49:18 +1000 Subject: [PATCH 2/2] don't log errors raise an exception, change sleep error to a warning --- discord_webhook/async_webhook.py | 9 +++++---- discord_webhook/webhook.py | 10 ++++++---- discord_webhook/webhook_exceptions.py | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/discord_webhook/async_webhook.py b/discord_webhook/async_webhook.py index d66bc73..955853d 100644 --- a/discord_webhook/async_webhook.py +++ b/discord_webhook/async_webhook.py @@ -6,6 +6,7 @@ from http.client import HTTPException from . import DiscordWebhook +from .webhook_exceptions import DiscordException logger = logging.getLogger(__name__) @@ -85,7 +86,7 @@ async def handle_rate_limit(self, response, request) -> "httpx.Response": if not response.headers.get("Via"): raise HTTPException(errors) wh_sleep = float(errors["retry_after"]) + 0.15 - logger.error( + logger.warning( "Webhook rate limited: sleeping for {wh_sleep} seconds...".format( wh_sleep=round(wh_sleep, 2) ) @@ -107,7 +108,7 @@ async def execute(self, remove_embeds=False) -> "httpx.Response": elif response.status_code == 429 and self.rate_limit_retry: response = await self.handle_rate_limit(response, self.api_post_request) else: - logger.error( + raise DiscordException( "Webhook status code {status_code}: {content}".format( status_code=response.status_code, content=response.content.decode("utf-8"), @@ -150,7 +151,7 @@ async def edit(self) -> "httpx.Response": response = await self.handle_rate_limit(response, request) logger.debug("Webhook edited") else: - logger.error( + raise DiscordException( "Webhook status code {status_code}: {content}".format( status_code=response.status_code, content=response.content.decode("utf-8"), @@ -175,7 +176,7 @@ async def delete(self) -> "httpx.Response": if response.status_code in [200, 204]: logger.debug("Webhook deleted") else: - logger.error( + raise DiscordException( "Webhook status code {status_code}: {content}".format( status_code=response.status_code, content=response.content.decode("utf-8"), diff --git a/discord_webhook/webhook.py b/discord_webhook/webhook.py index 32ee471..a63dcef 100644 --- a/discord_webhook/webhook.py +++ b/discord_webhook/webhook.py @@ -376,7 +376,7 @@ def json(self) -> Dict[str, Any]: } embeds_empty = not any(data["embeds"]) if "embeds" in data else True if embeds_empty and "content" not in data and bool(self.files) is False: - logger.error("webhook message is empty! set content or embed data") + raise DiscordException("webhook message is empty! set content or embed data") return data def api_post_request(self) -> "requests.Response": @@ -414,8 +414,10 @@ def handle_rate_limit(self, response, request): if not response.headers.get("Via"): raise HTTPException(errors) wh_sleep = float(errors["retry_after"]) + 0.15 - logger.error( - f"Webhook rate limited: sleeping for {wh_sleep:.2f} seconds..." + logger.warning( + "Webhook rate limited: sleeping for {wh_sleep} seconds...".format( + wh_sleep=round(wh_sleep, 2) + ) ) time.sleep(wh_sleep) response = request() @@ -501,7 +503,7 @@ def edit(self) -> "requests.Response": response = self.handle_rate_limit(response, request) logger.debug("Webhook edited") else: - logger.error( + raise DiscordException( "Webhook status code {status_code}: {content}".format( status_code=response.status_code, content=response.content.decode("utf-8"), diff --git a/discord_webhook/webhook_exceptions.py b/discord_webhook/webhook_exceptions.py index e5b5dde..81e5b2f 100644 --- a/discord_webhook/webhook_exceptions.py +++ b/discord_webhook/webhook_exceptions.py @@ -17,6 +17,7 @@ def __init__(self, color: Union[str, int], message=None) -> None: ) super().__init__(message) + class DiscordException(Exception): """ This Exception is throw as a generic error from discord's API. @@ -24,4 +25,3 @@ class DiscordException(Exception): def __init__(self, *args: object) -> None: super().__init__(*args) -