Skip to content

Commit

Permalink
Add ack behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
cicdw committed Sep 7, 2024
1 parent bbe4b69 commit 01a5838
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/prefect/blocks/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ async def notify(
subject: Optional[str] = None,
):
if not self.allow_private_urls:
validate_restricted_url(self.url.get_secret_value())
try:
validate_restricted_url(self.url.get_secret_value())
except ValueError as exc:
if self._raise_on_failure:
raise NotificationError(str(exc))
raise

await super().notify(body, subject)

Expand Down
22 changes: 22 additions & 0 deletions tests/blocks/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
import respx

from prefect.blocks.abstract import NotificationError
from prefect.blocks.notifications import (
PREFECT_NOTIFY_TYPE_DEFAULT,
AppriseNotificationBlock,
Expand Down Expand Up @@ -126,6 +127,27 @@ async def test_notification_can_prevent_restricted_urls(
with pytest.raises(ValueError, match=f"is not a valid URL.*{reason}"):
await notification.notify(subject="example", body="example")

async def test_raises_on_url_validation_failure(self, block_class):
"""
When within a raise_on_failure block, we want URL validation errors to be
wrapped and captured as NotificationErrors for reporting back to users.
"""
block = block_class(url="https://127.0.0.1/foo/bar", allow_private_urls=False)

# outside of a raise_on_failure block, we get a ValueError directly
with pytest.raises(ValueError, match="not a valid URL") as captured:
await block.notify(subject="Test", body="Test")

# inside of a raise_on_failure block, we get a NotificationError
with block.raise_on_failure():
with pytest.raises(NotificationError) as captured:
await block.notify(subject="Test", body="Test")

assert captured.value.log == (
"'https://127.0.0.1/foo/bar' is not a valid URL. It resolves to the "
"private address 127.0.0.1."
)


class TestMattermostWebhook:
async def test_notify_async(self):
Expand Down

0 comments on commit 01a5838

Please sign in to comment.