Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(polar): classify exhausted 5xx retries as self-recovering (#121830)

Tom Owersb4656ba1b7dd
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/polar/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/polar/source.py
index 66d52727d8fd..4c048e8d7b99 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/polar/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/polar/source.py
@@ -101,6 +101,21 @@ class PolarSource(ResumableSource[PolarSourceConfig, PolarResumeConfig]):
"404 Client Error: Not Found for url: https://api.polar.sh": "The Polar API version this source uses is no longer supported by Polar. Please contact support to move it to a newer version.",
}
+ def get_retryable_errors(self) -> set[str]:
+ # `get_rows` calls `response.raise_for_status()` directly rather than through the shared REST
+ # engine, so a 5xx that survives `_get_polar_session`'s `DEFAULT_RETRY` (status_forcelist
+ # 429/500/502/503/504) isn't wrapped as a retryable exception type either — it's still a
+ # transient Polar-side outage, and Temporal retries the whole activity next. Match status,
+ # reason and host, not the path or query string, since every endpoint and page can hit this.
+ # Mirrors Microsoft Clarity's and Checkout.com's equivalent classification of the same
+ # already-retried status codes.
+ return {
+ "500 Server Error: Internal Server Error for url: https://api.polar.sh",
+ "502 Server Error: Bad Gateway for url: https://api.polar.sh",
+ "503 Server Error: Service Unavailable for url: https://api.polar.sh",
+ "504 Server Error: Gateway Timeout for url: https://api.polar.sh",
+ }
+
def validate_credentials(
self, config: PolarSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
) -> tuple[bool, str | None]:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/polar/test_polar_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/polar/test_polar_source.py
index 39422a3efffa..38cb1f144872 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/polar/test_polar_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/polar/test_polar_source.py
@@ -6,6 +6,7 @@ from unittest.mock import MagicMock, patch
import requests
from parameterized import parameterized
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import error_message_matches
from products.warehouse_sources.backend.temporal.data_imports.sources.common.http import DEFAULT_RETRY
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.polar import PolarSourceConfig
from products.warehouse_sources.backend.temporal.data_imports.sources.polar import polar as polar_module
@@ -47,6 +48,47 @@ class TestGetPolarSession:
assert session.adapters["https://"].max_retries is DEFAULT_RETRY
+class TestPolarRetryableErrors:
+ @parameterized.expand(
+ [
+ (
+ "service_unavailable",
+ "503 Server Error: Service Unavailable for url: https://api.polar.sh/v1/orders/?limit=100&page=1&sorting=created_at",
+ True,
+ ),
+ (
+ "bad_gateway",
+ "502 Server Error: Bad Gateway for url: https://api.polar.sh/v1/customers/?limit=100&page=1",
+ True,
+ ),
+ (
+ "internal_server_error",
+ "500 Server Error: Internal Server Error for url: https://api.polar.sh/v1/products/?limit=100&page=1",
+ True,
+ ),
+ (
+ "gateway_timeout",
+ "504 Server Error: Gateway Timeout for url: https://api.polar.sh/v1/subscriptions/?limit=100&page=1",
+ True,
+ ),
+ (
+ "different_host",
+ "503 Server Error: Service Unavailable for url: https://example.com/v1/orders/?limit=100&page=1",
+ False,
+ ),
+ (
+ "unauthorized",
+ "401 Client Error: Unauthorized for url: https://api.polar.sh/v1/orders/?limit=100&page=1",
+ False,
+ ),
+ ]
+ )
+ def test_get_retryable_errors_match_only_polar_server_errors(
+ self, _name: str, error_msg: str, retryable: bool
+ ) -> None:
+ assert error_message_matches(error_msg, PolarSource().get_retryable_errors()) is retryable
+
+
class TestPolarValidateCredentials:
def test_invalid_token_maps_401_without_leaking_url(self) -> None:
session = MagicMock()