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(open-exchange-rates): tell users when the app id is rejected (#115009)

Tom Owersd653982c2cfe
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/open_exchange_rates.py b/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/open_exchange_rates.py
index c52581709299..cf11a0242e9c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/open_exchange_rates.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/open_exchange_rates.py
@@ -8,6 +8,8 @@ import requests
from structlog.types import FilteringBoundLogger
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
+from posthog.exceptions_capture import capture_exception
+
from products.warehouse_sources.backend.temporal.data_imports.sources.common.http import make_tracked_session
from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager
from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceResponse
@@ -87,16 +89,38 @@ def _error_description(response: requests.Response) -> str:
return ""
-def validate_credentials(app_id: str) -> bool:
+INVALID_APP_ID_ERROR = (
+ "Your Open Exchange Rates App ID is invalid or has been revoked. Create a new App ID in your Open "
+ "Exchange Rates dashboard, then reconnect."
+)
+RESTRICTED_ACCOUNT_ERROR = (
+ "Your Open Exchange Rates account is restricted. Check your account status in your Open Exchange "
+ "Rates dashboard, then reconnect."
+)
+VALIDATION_UNAVAILABLE_ERROR = (
+ "PostHog couldn't reach Open Exchange Rates to check your App ID. Wait a few minutes, then try again."
+)
+
+
+def validate_credentials(app_id: str) -> tuple[bool, str | None]:
"""Confirm the App ID is genuine with the cheapest authenticated call (/usage.json). It requires
a valid App ID (an invalid one returns 401) but is free and does not count toward the request
quota. A valid App ID returns 200."""
try:
session = make_tracked_session(headers=_auth_headers(app_id), redact_values=(app_id,) if app_id else ())
response = session.get(_build_url("usage.json"), timeout=10)
- return response.status_code == 200
- except Exception:
- return False
+ except Exception as e:
+ capture_exception(e)
+ return False, VALIDATION_UNAVAILABLE_ERROR
+
+ if response.status_code == 200:
+ return True, None
+ if response.status_code == 401:
+ return False, INVALID_APP_ID_ERROR
+ # Open Exchange Rates answers 403 for a suspended account and 429 for one blocked after over-use.
+ if response.status_code in (403, 429):
+ return False, RESTRICTED_ACCOUNT_ERROR
+ return False, VALIDATION_UNAVAILABLE_ERROR
def _iter_currencies(data: dict[str, Any]) -> list[dict[str, Any]]:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/source.py
index 51e5dba6cacf..2f9ff897c79c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/source.py
@@ -19,6 +19,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.generated_
OpenExchangeRatesSourceConfig,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.open_exchange_rates.open_exchange_rates import (
+ INVALID_APP_ID_ERROR,
OpenExchangeRatesResumeConfig,
open_exchange_rates_source,
validate_credentials as validate_open_exchange_rates_credentials,
@@ -96,7 +97,7 @@ The free plan is restricted to the `USD` base currency — a custom base currenc
# An invalid, missing, or revoked App ID surfaces as a 401 when `_request` raises. Retrying
# can never fix a credential problem, so stop the sync. Match the stable status text and
# base host, not the per-request path.
- "401 Client Error: Unauthorized for url: https://openexchangerates.org/api": "Your Open Exchange Rates App ID is invalid or has been revoked. Create a new App ID in your Open Exchange Rates dashboard, then reconnect.",
+ "401 Client Error: Unauthorized for url: https://openexchangerates.org/api": INVALID_APP_ID_ERROR,
"403 Client Error: Forbidden for url: https://openexchangerates.org/api": "Your Open Exchange Rates App ID is missing or your account is restricted. Check the App ID and your account status, then reconnect.",
# Open Exchange Rates returns 429 `not_allowed` for plan-gated features (e.g. a non-USD base
# currency on the free plan), not a transient throttle — so it is permanent, not retryable.
@@ -137,15 +138,7 @@ The free plan is restricted to the `USD` base currency — a custom base currenc
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
- if validate_open_exchange_rates_credentials(config.app_id):
- return True, None
-
- # The probe also returns False on transient network/timeout errors, so don't claim the App ID
- # is definitively invalid — point at both possibilities.
- return (
- False,
- "Unable to verify your Open Exchange Rates App ID. Check that the App ID is correct and that openexchangerates.org is reachable.",
- )
+ return validate_open_exchange_rates_credentials(config.app_id)
def get_resumable_source_manager(
self, inputs: SourceInputs
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/tests/test_open_exchange_rates.py b/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/tests/test_open_exchange_rates.py
index 275e3b5dea30..ed3566d0130a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/tests/test_open_exchange_rates.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/tests/test_open_exchange_rates.py
@@ -85,20 +85,36 @@ class TestRequest:
class TestValidateCredentials:
- @parameterized.expand([("valid", 200, True), ("unauthorized", 401, False)])
+ @parameterized.expand(
+ [
+ ("valid", 200, (True, None)),
+ ("unauthorized", 401, (False, open_exchange_rates.INVALID_APP_ID_ERROR)),
+ ("suspended", 403, (False, open_exchange_rates.RESTRICTED_ACCOUNT_ERROR)),
+ ("over_use", 429, (False, open_exchange_rates.RESTRICTED_ACCOUNT_ERROR)),
+ ("upstream_outage", 503, (False, open_exchange_rates.VALIDATION_UNAVAILABLE_ERROR)),
+ ]
+ )
@mock.patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.open_exchange_rates.open_exchange_rates.make_tracked_session"
)
- def test_status_mapping(self, _name: str, status: int, expected: bool, mock_session: mock.MagicMock) -> None:
+ def test_status_mapping(
+ self, _name: str, status: int, expected: tuple[bool, str | None], mock_session: mock.MagicMock
+ ) -> None:
mock_session.return_value.get.return_value = _FakeResponse(status_code=status)
- assert validate_credentials("key") is expected
+ assert validate_credentials("key") == expected
+ @mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.open_exchange_rates.open_exchange_rates.capture_exception"
+ )
@mock.patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.open_exchange_rates.open_exchange_rates.make_tracked_session"
)
- def test_exception_returns_false(self, mock_session: mock.MagicMock) -> None:
+ def test_network_error_does_not_blame_the_app_id(
+ self, mock_session: mock.MagicMock, mock_capture: mock.MagicMock
+ ) -> None:
mock_session.return_value.get.side_effect = requests.ConnectionError("boom")
- assert validate_credentials("key") is False
+ assert validate_credentials("key") == (False, open_exchange_rates.VALIDATION_UNAVAILABLE_ERROR)
+ mock_capture.assert_called_once()
@mock.patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.open_exchange_rates.open_exchange_rates.make_tracked_session"
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/tests/test_open_exchange_rates_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/tests/test_open_exchange_rates_source.py
index e33787487064..d181f0b81bf4 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/tests/test_open_exchange_rates_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/open_exchange_rates/tests/test_open_exchange_rates_source.py
@@ -27,11 +27,11 @@ class TestOpenExchangeRatesSource:
@pytest.mark.parametrize(
"mock_return, expected_valid, expected_message",
[
- (True, True, None),
+ ((True, None), True, None),
(
+ (False, "Your Open Exchange Rates App ID is invalid"),
False,
- False,
- "Unable to verify your Open Exchange Rates App ID. Check that the App ID is correct and that openexchangerates.org is reachable.",
+ "Your Open Exchange Rates App ID is invalid",
),
],
)
@@ -39,7 +39,11 @@ class TestOpenExchangeRatesSource:
"products.warehouse_sources.backend.temporal.data_imports.sources.open_exchange_rates.source.validate_open_exchange_rates_credentials"
)
def test_validate_credentials(
- self, mock_validate: mock.MagicMock, mock_return: bool, expected_valid: bool, expected_message: str | None
+ self,
+ mock_validate: mock.MagicMock,
+ mock_return: tuple[bool, str | None],
+ expected_valid: bool,
+ expected_message: str | None,
) -> None:
mock_validate.return_value = mock_return