Skip to content

Commit

Permalink
added status codes to the webhook clients error response logs
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaKerman committed Nov 19, 2024
1 parent 6a8a491 commit 79c36cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
11 changes: 6 additions & 5 deletions elementary/clients/slack/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from slack_sdk import WebClient, WebhookClient
from slack_sdk.errors import SlackApiError
from slack_sdk.http_retry.builtin_handlers import RateLimitErrorRetryHandler
from slack_sdk.webhook.webhook_response import WebhookResponse

from elementary.clients.slack.schema import SlackMessageSchema
from elementary.config.config import Config
Expand Down Expand Up @@ -95,9 +96,9 @@ def send_message(
channel=channel_name,
text=message.text,
blocks=json.dumps(message.blocks) if message.blocks else None,
attachments=json.dumps(message.attachments)
if message.attachments
else None,
attachments=(
json.dumps(message.attachments) if message.attachments else None
),
)
return True
except SlackApiError as err:
Expand Down Expand Up @@ -235,15 +236,15 @@ def _initial_client(self):
@sleep_and_retry
@limits(calls=1, period=ONE_SECOND)
def send_message(self, message: SlackMessageSchema, **kwargs) -> bool:
response = self.client.send(
response: WebhookResponse = self.client.send(
text=message.text, blocks=message.blocks, attachments=message.attachments
)
if response.status_code == OK_STATUS_CODE:
return True

else:
logger.error(
f"Could not post message to slack via webhook - {self.webhook}. Error: {response.text}"
f"Could not post message to slack via webhook - {self.webhook}. Status code: {response.status_code}, Error: {response.body}"
)
return False

Expand Down
7 changes: 3 additions & 4 deletions elementary/clients/teams/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pymsteams import cardsection, connectorcard, potentialaction # type: ignore
from ratelimit import limits, sleep_and_retry
from requests import Response

from elementary.config.config import Config
from elementary.tracking.tracking_interface import Tracking
Expand Down Expand Up @@ -67,15 +68,13 @@ def _initial_client(self):
@limits(calls=1, period=ONE_SECOND)
def send_message(self, **kwargs) -> bool:
self.client.send()
response = self.client.last_http_response
response: Response = self.client.last_http_response

if response.status_code == OK_STATUS_CODE:
return True
else:
logger.error(
"Could not post message to teams via webhook - %s. Error: %s",
{self.webhook},
{response.text},
f"Could not post message to teams via webhook - {self.webhook}. Status code: {response.status_code}, Error: {response.text}"
)
return False

Expand Down

0 comments on commit 79c36cd

Please sign in to comment.