Skip to content

Commit 7c0bf27

Browse files
authored
fix: log warning for non-2xx webhook responses (#6732)
1 parent 8dc90f6 commit 7c0bf27

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

api/tests/unit/webhooks/test_unit_webhooks.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,37 @@ def test_call_organisation_webhooks__multiple_webhooks__failure__calls_expected(
265265
)
266266

267267

268+
def test_call_webhook_with_failure_mail_after_retries__non_2xx_response__logs_warning(
269+
mocker: MockerFixture, organisation: Organisation
270+
) -> None:
271+
# Given
272+
mock_response = MagicMock()
273+
mock_response.ok = False
274+
mock_response.status_code = 301
275+
mocker.patch("webhooks.webhooks.requests.post", return_value=mock_response)
276+
mock_logger = mocker.patch("webhooks.webhooks.logger")
277+
278+
webhook = OrganisationWebhook.objects.create(
279+
url="http://url.1.com", enabled=True, organisation=organisation
280+
)
281+
282+
# When
283+
call_webhook_with_failure_mail_after_retries(
284+
webhook.id,
285+
data={},
286+
webhook_type=WebhookType.ORGANISATION.value,
287+
)
288+
289+
# Then
290+
mock_logger.warning.assert_called_once_with(
291+
"Webhook %d returned HTTP %d (attempt %d/%d)",
292+
webhook.id,
293+
301,
294+
1,
295+
3,
296+
)
297+
298+
268299
def test_call_webhook_with_failure_mail_after_retries_raises_error_on_invalid_args(): # type: ignore[no-untyped-def]
269300
try_count = 10
270301
with pytest.raises(ValueError):

api/webhooks/webhooks.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,14 @@ def call_webhook_with_failure_mail_after_retries( # type: ignore[no-untyped-def
190190
res = requests.post(
191191
str(webhook.url), data=json_data, headers=headers, timeout=10
192192
)
193-
res.raise_for_status()
193+
if not res.ok:
194+
logger.warning(
195+
"Webhook %d returned HTTP %d (attempt %d/%d)",
196+
webhook_id,
197+
res.status_code,
198+
try_count,
199+
max_retries,
200+
)
194201
except requests.exceptions.RequestException as exc:
195202
logger.warning(
196203
"Webhook call failed for webhook %d (attempt %d/%d): %s",

0 commit comments

Comments
 (0)