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(medusa): show the friendly server url error in the source wizard (#115253)

Tom Owers114e67aee1b7
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/medusa/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/medusa/source.py
index b5d802761874..c6aedb57a798 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/medusa/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/medusa/source.py
@@ -41,6 +41,10 @@ CAPTION = """Sync orders, products, customers and more from your self-hosted Med
Requires Medusa v2. Create a secret API key in your Medusa Admin dashboard under **Settings > Secret API Keys**, then enter it here with your server's URL. Publishable API keys only work with the Store API and will not work here."""
REJECTED_KEY_MESSAGE = "Medusa rejected the API key. Check that you're using a secret API key from a Medusa v2 server, not a publishable key."
+HTTPS_REQUIRED_MESSAGE = "The Medusa server URL must use HTTPS so your API key is not sent in the clear. Update the server URL and reconnect."
+INVALID_URL_MESSAGE = (
+ "The Medusa server URL is invalid. Enter your server's public URL, e.g. https://store.example.com, and reconnect."
+)
@SourceRegistry.register
@@ -102,8 +106,8 @@ class MedusaSource(ResumableSource[MedusaSourceConfig, MedusaResumeConfig], Vali
return {
"401 Client Error": f"{REJECTED_KEY_MESSAGE} Then reconnect.",
"403 Client Error": "The API key does not have access to this data. Check the key in your Medusa Admin dashboard, then reconnect.",
- HTTPS_REQUIRED_ERROR: "The Medusa server URL must use HTTPS so your API key is not sent in the clear. Update the server URL and reconnect.",
- INVALID_URL_ERROR: "The Medusa server URL is invalid. Enter your server's public URL, e.g. https://store.example.com, and reconnect.",
+ HTTPS_REQUIRED_ERROR: HTTPS_REQUIRED_MESSAGE,
+ INVALID_URL_ERROR: INVALID_URL_MESSAGE,
PAGINATION_LIMIT_ERROR: "Medusa kept returning pages without reaching the total it reported. Check that the server URL points at your Medusa server.",
}
@@ -137,7 +141,8 @@ class MedusaSource(ResumableSource[MedusaSourceConfig, MedusaResumeConfig], Vali
try:
hostname = hostname_of(config.base_url)
except ValueError as e:
- return False, str(e)
+ # The raw error can echo the pasted URL back, credentials included.
+ return False, HTTPS_REQUIRED_MESSAGE if str(e) == HTTPS_REQUIRED_ERROR else INVALID_URL_MESSAGE
host_valid, host_error = self.is_database_host_valid(hostname, team_id)
if not host_valid:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/medusa/tests/test_medusa_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/medusa/tests/test_medusa_source.py
index 0df7b4bd1f8f..496b315fdb2b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/medusa/tests/test_medusa_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/medusa/tests/test_medusa_source.py
@@ -5,7 +5,11 @@ from unittest import mock
from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceInputs
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.medusa import MedusaSourceConfig
-from products.warehouse_sources.backend.temporal.data_imports.sources.medusa.source import MedusaSource
+from products.warehouse_sources.backend.temporal.data_imports.sources.medusa.source import (
+ HTTPS_REQUIRED_MESSAGE,
+ INVALID_URL_MESSAGE,
+ MedusaSource,
+)
SOURCE_MODULE = "products.warehouse_sources.backend.temporal.data_imports.sources.medusa.source"
@@ -41,20 +45,21 @@ class TestMedusaSource:
assert self.source.connection_host_fields == ["base_url"]
@pytest.mark.parametrize(
- "base_url",
+ ("base_url", "expected_message"),
[
- "",
- "http://store.example.com",
- "https://user:[email protected]",
+ ("", INVALID_URL_MESSAGE),
+ ("http://store.example.com", HTTPS_REQUIRED_MESSAGE),
+ ("https://user:[email protected]", INVALID_URL_MESSAGE),
],
)
- def test_validate_credentials_rejects_unsafe_urls_before_probing(self, base_url: str) -> None:
+ def test_validate_credentials_rejects_unsafe_urls_before_probing(
+ self, base_url: str, expected_message: str
+ ) -> None:
config = MedusaSourceConfig(base_url=base_url, api_key="sk_test")
with mock.patch(f"{SOURCE_MODULE}.validate_medusa_credentials") as mock_probe:
is_valid, message = self.source.validate_credentials(config, self.team_id)
- assert is_valid is False
- assert message
+ assert (is_valid, message) == (False, expected_message)
mock_probe.assert_not_called()
def test_validate_credentials_rejects_disallowed_hosts_before_probing(self) -> None: