Create log record for telegram formatting error in notification (#3628)
This commit is contained in:
parent
82b5a877d9
commit
d57b41b758
4 changed files with 57 additions and 2 deletions
|
|
@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
- Move Insights to OnCall as a separate page ([#2382](https://github.com/grafana/oncall-private/issues/2382))
|
||||
- Allow mobile app to access paging endpoint @imtoori ([#3619](https://github.com/grafana/oncall/pull/3619))
|
||||
- Create log record when there is a telegram formatting error in notification ([#3628](https://github.com/grafana/oncall/pull/3628))
|
||||
|
||||
### Fixed
|
||||
|
||||
|
|
|
|||
|
|
@ -70,7 +70,8 @@ class UserNotificationPolicyLogRecord(models.Model):
|
|||
ERROR_NOTIFICATION_FORBIDDEN,
|
||||
ERROR_NOTIFICATION_TELEGRAM_USER_IS_DEACTIVATED,
|
||||
ERROR_NOTIFICATION_MOBILE_USER_HAS_NO_ACTIVE_DEVICE,
|
||||
) = range(28)
|
||||
ERROR_NOTIFICATION_FORMATTING_ERROR,
|
||||
) = range(29)
|
||||
|
||||
# for this errors we want to send message to general log channel
|
||||
ERRORS_TO_SEND_IN_SLACK_CHANNEL = [
|
||||
|
|
@ -270,6 +271,8 @@ class UserNotificationPolicyLogRecord(models.Model):
|
|||
== UserNotificationPolicyLogRecord.ERROR_NOTIFICATION_MOBILE_USER_HAS_NO_ACTIVE_DEVICE
|
||||
):
|
||||
result += f"failed to send push notification to {user_verbal} because user has no device set up"
|
||||
elif self.notification_error_code == UserNotificationPolicyLogRecord.ERROR_NOTIFICATION_FORMATTING_ERROR:
|
||||
result += f"failed to send message to {user_verbal} due to a formatting error"
|
||||
else:
|
||||
# TODO: handle specific backend errors
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -113,6 +113,13 @@ class TelegramToUserConnector(models.Model):
|
|||
alert_group=alert_group,
|
||||
)
|
||||
except error.BadRequest:
|
||||
TelegramToUserConnector.create_telegram_notification_error(
|
||||
alert_group,
|
||||
self.user,
|
||||
notification_policy,
|
||||
UserNotificationPolicyLogRecord.ERROR_NOTIFICATION_FORMATTING_ERROR,
|
||||
reason="Notification sent but there was a formatting error in the rendered template",
|
||||
)
|
||||
telegram_client.send_message(
|
||||
chat_id=self.telegram_chat_id,
|
||||
message_type=TelegramMessage.FORMATTING_ERROR,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from unittest.mock import patch
|
||||
from unittest.mock import call, patch
|
||||
|
||||
import pytest
|
||||
from telegram import error
|
||||
|
|
@ -186,3 +186,47 @@ def test_personal_connector_send_full_alert_group(
|
|||
|
||||
log_record = notification_policy.personal_log_records.last()
|
||||
assert log_record.type == UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_SUCCESS
|
||||
|
||||
|
||||
@patch.object(TelegramClient, "send_message", side_effect=[error.BadRequest("error"), None])
|
||||
@pytest.mark.django_db
|
||||
def test_personal_connector_send_full_alert_group_formatting_error(
|
||||
mock_send_message,
|
||||
make_organization_and_user,
|
||||
make_telegram_user_connector,
|
||||
make_user_notification_policy,
|
||||
make_alert_receive_channel,
|
||||
make_alert_group,
|
||||
make_alert,
|
||||
):
|
||||
# set up a user with Telegram account connected
|
||||
organization, user = make_organization_and_user()
|
||||
make_telegram_user_connector(user)
|
||||
notification_policy = make_user_notification_policy(
|
||||
user,
|
||||
UserNotificationPolicy.Step.NOTIFY,
|
||||
notify_by=UserNotificationPolicy.NotificationChannel.TELEGRAM,
|
||||
important=False,
|
||||
)
|
||||
|
||||
# create an alert group with an existing Telegram message in channel
|
||||
alert_receive_channel = make_alert_receive_channel(organization)
|
||||
alert_group = make_alert_group(alert_receive_channel)
|
||||
make_alert(alert_group=alert_group, raw_request_data=alert_receive_channel.config.example_payload)
|
||||
|
||||
user.telegram_connection.send_full_alert_group(alert_group, notification_policy)
|
||||
|
||||
failed_call, notification_call = mock_send_message.mock_calls
|
||||
assert failed_call == call(
|
||||
chat_id=user.telegram_connection.telegram_chat_id,
|
||||
message_type=TelegramMessage.PERSONAL_MESSAGE,
|
||||
alert_group=alert_group,
|
||||
)
|
||||
assert notification_call == call(
|
||||
chat_id=user.telegram_connection.telegram_chat_id,
|
||||
message_type=TelegramMessage.FORMATTING_ERROR,
|
||||
alert_group=alert_group,
|
||||
)
|
||||
|
||||
log_record = notification_policy.personal_log_records.last()
|
||||
assert log_record.type == UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_FAILED
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue