Skip to content

Commit a0bc5d9

Browse files
author
Frank Tubbing
committed
Error handling for the teams webhook alerting to handle cases like network exceptions
1 parent 047db0d commit a0bc5d9

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

elementary/messages/messaging_integrations/teams_webhook.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,28 @@ def send_message(
6969
body: MessageBody,
7070
) -> MessageSendResult[EmptyMessageContext]:
7171
card = format_adaptive_card(body)
72-
response = send_adaptive_card(self.url, card)
73-
# The status code is not reliable for the older version and 202 for the newer version
74-
# The response returns 1 in the body for the old version for success and otherwise sometext
75-
# The new version is not reliable to do a check and in the response there is nothing that can be used
76-
# to determine if the message was sent successfully.
77-
if (
78-
response.status_code not in (HTTPStatus.OK, HTTPStatus.ACCEPTED)
79-
or len(response.text) > 1
80-
):
81-
raise MessagingIntegrationError(
82-
f"Could not post message to Teams via webhook - {self.url}. Status code: {response.status_code}, Error: {response.text}"
72+
try:
73+
response = send_adaptive_card(self.url, card)
74+
# The status code is not reliable for the older version and 202 for the newer version
75+
# The response returns 1 in the body for the old version for success and otherwise some text
76+
# The new version is not reliable to do a check and in the response there is nothing that can be used
77+
# to determine if the message was sent successfully.
78+
if (
79+
response.status_code not in (HTTPStatus.OK, HTTPStatus.ACCEPTED)
80+
or len(response.text) > 1
81+
):
82+
raise MessagingIntegrationError(
83+
f"Could not post message to Teams via webhook. Status code: {response.status_code}, Error: {response.text}"
84+
)
85+
return MessageSendResult(
86+
message_context=EmptyMessageContext(),
87+
timestamp=datetime.utcnow(),
88+
message_format="adaptive_cards",
8389
)
84-
return MessageSendResult(
85-
message_context=EmptyMessageContext(),
86-
timestamp=datetime.utcnow(),
87-
message_format="adaptive_cards",
88-
)
90+
except requests.RequestException as e:
91+
raise MessagingIntegrationError(
92+
f"Network error while posting message to Teams webhook: {str(e)}"
93+
) from e
8994

9095
def supports_reply(self) -> bool:
9196
return False

elementary/monitor/dbt_project/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ target/
33
dbt_modules/
44
dbt_packages*/
55
logs/
6+
dbt_internal_packages/

0 commit comments

Comments
 (0)