Skip to content

Commit bdb07a1

Browse files
committed
Catch agnostic HTTP error types in prefect and swap the pagination raise
Widen PrefectClient.http_exceptions to admit the backend-agnostic HTTP error types alongside the requests ones, and raise HTTPInvalidURLError instead of requests' InvalidURL from check_pagination_url so the check stays correct after the httpx migration.
1 parent f1e1514 commit bdb07a1

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

prefect/datadog_checks/prefect/check.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
from datadog_checks.base import AgentCheck
1616
from datadog_checks.base.utils.common import pattern_filter
17+
from datadog_checks.base.utils.http_exceptions import (
18+
HTTPConnectionError,
19+
HTTPInvalidURLError,
20+
HTTPStatusError,
21+
HTTPTimeoutError,
22+
)
1723

1824
from .config_models import ConfigMixin
1925
from .metrics import METRICS_SPEC
@@ -666,7 +672,17 @@ class PrefectClient:
666672
"""HTTP client wrapping GET/POST requests and pagination for the Prefect API."""
667673

668674
def __init__(self, url: str, http: RequestsWrapper, log: CheckLoggingAdapter):
669-
self.http_exceptions = (HTTPError, InvalidURL, ConnectionError, Timeout, JSONDecodeError)
675+
self.http_exceptions = (
676+
HTTPError,
677+
InvalidURL,
678+
ConnectionError,
679+
Timeout,
680+
JSONDecodeError,
681+
HTTPStatusError,
682+
HTTPInvalidURLError,
683+
HTTPConnectionError,
684+
HTTPTimeoutError,
685+
)
670686
self.url = url
671687
parsed_url = urlparse(url)
672688
self.base_url_scheme = parsed_url.scheme
@@ -696,7 +712,7 @@ def post(self, endpoint: str, payload: dict | None = None) -> Any:
696712
def check_pagination_url(self, next_page: str):
697713
parsed_next_page = urlparse(next_page)
698714
if parsed_next_page.scheme != self.base_url_scheme or parsed_next_page.netloc != self.base_url_netloc:
699-
raise InvalidURL(f'Invalid next_page URL with unexpected host: {next_page}')
715+
raise HTTPInvalidURLError(f'Invalid next_page URL with unexpected host: {next_page}')
700716

701717
def paginate_filter(self, endpoint: str, payload: dict | None = None) -> list[dict]:
702718
"""Implements pagination for /filter endpoints using limit/offset loop."""

prefect/tests/conftest.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,29 @@ def mock_prefect_client(mocker):
133133

134134
from requests.exceptions import ConnectionError, HTTPError, InvalidURL, Timeout
135135

136+
from datadog_checks.base.utils.http_exceptions import (
137+
HTTPConnectionError,
138+
HTTPInvalidURLError,
139+
HTTPStatusError,
140+
HTTPTimeoutError,
141+
)
136142
from datadog_checks.prefect.check import PrefectClient
137143

138144
get_responses = _load_fixture("get_metrics.json")
139145
post_responses = _load_fixture("post_metrics.json")
140146

141147
mock_client = create_autospec(PrefectClient, instance=True)
142-
mock_client.http_exceptions = (HTTPError, InvalidURL, ConnectionError, Timeout, JSONDecodeError)
148+
mock_client.http_exceptions = (
149+
HTTPError,
150+
InvalidURL,
151+
ConnectionError,
152+
Timeout,
153+
JSONDecodeError,
154+
HTTPStatusError,
155+
HTTPInvalidURLError,
156+
HTTPConnectionError,
157+
HTTPTimeoutError,
158+
)
143159

144160
mock_client.get.side_effect = lambda endpoint, **kwargs: get_responses[endpoint]
145161
mock_client.paginate_filter.side_effect = lambda endpoint, payload=None: post_responses[endpoint]

prefect/tests/test_unit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from unittest.mock import Mock
44

55
import pytest
6-
from requests.exceptions import InvalidURL
76

87
from datadog_checks.base.stubs.aggregator import AggregatorStub
8+
from datadog_checks.base.utils.http_exceptions import HTTPInvalidURLError
99
from datadog_checks.dev.utils import get_metadata_metrics
1010
from datadog_checks.prefect import PrefectCheck
1111
from datadog_checks.prefect.check import PrefectClient
@@ -632,7 +632,7 @@ def test_paginate_events_rejects_external_next_page():
632632
log.error.assert_called_once()
633633
args = log.error.call_args[0]
634634
assert args[0] == "Could not collect next page of events: %s, data is incomplete"
635-
assert isinstance(args[1], InvalidURL)
635+
assert isinstance(args[1], HTTPInvalidURLError)
636636
assert str(args[1]) == "Invalid next_page URL with unexpected host: http://attacker.example/evil"
637637

638638

0 commit comments

Comments
 (0)