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

raise exception instead of log an error #150

Open
wants to merge 2 commits into
base: master
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
9 changes: 5 additions & 4 deletions discord_webhook/async_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from http.client import HTTPException

from . import DiscordWebhook
from .webhook_exceptions import DiscordException

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -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)
)
Expand All @@ -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"),
Expand Down Expand Up @@ -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"),
Expand All @@ -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"),
Expand Down
14 changes: 8 additions & 6 deletions discord_webhook/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -448,7 +450,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"),
Expand Down Expand Up @@ -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"),
Expand Down
9 changes: 9 additions & 0 deletions discord_webhook/webhook_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)