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(stripe): stop showing stripe's raw request error when validation fails (#119168)

Tom Owersf7d5a17eb6a0
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py
index 215f3d9427a3..c426597b6036 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py
@@ -561,21 +561,19 @@ If automatic creation failed with a permissions error, the fix depends on how yo
"Couldn't reach Stripe to validate your credentials. This is usually temporary. Please try again in a few minutes.",
)
except StripeValidationError as e:
- # Non-403, non-transient failures (e.g. an unexpected schema or response error) are not
- # configuration issues, so surface the underlying Stripe message verbatim — the cause
- # isn't obvious from the resource name. Transient 5xx/connection/rate-limit failures are
- # handled by the StripeTransientError branch above. Fold any 403s collected before the
- # unknown error into the same toast. Guard against empty / whitespace-only error strings
- # so we never crash the response path while reporting a different error.
- def _first_line(msg: str) -> str:
- lines = (msg or "").splitlines()
- return lines[0][:200] if lines else "(no detail)"
-
- details = "; ".join(f"{name}: {_first_line(msg)}" for name, msg in e.errors.items())
- message = f"Stripe validation failed — {details}"
- if e.missing_permissions:
- message += f". Additionally lacks permissions for {', '.join(e.missing_permissions.keys())}"
- return False, message
+ # Non-403, non-transient failures. Stripe's own text names request parameters the user
+ # never controls, so map known rejections to their curated copy and send the rest to
+ # error tracking behind a generic next step.
+ for msg in e.errors.values():
+ for pattern, friendly in self.get_non_retryable_errors().items():
+ if friendly and pattern in msg:
+ return False, friendly
+ capture_exception(e, {"stripe_errors": e.errors, "missing_permissions": e.missing_permissions})
+ return (
+ False,
+ "Stripe couldn't read your account with this API key. Check that the account is active in "
+ "your Stripe dashboard or contact Stripe support, then reconnect.",
+ )
except ValueError as e:
# `_get_api_key` raises ValueError for deterministic config problems (missing API key,
# missing integration ID, missing access token). The user-facing wording already lives
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
index dafbe939aeff..268c1f6290c1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
@@ -95,6 +95,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.stripe.str
StripeNestedResource,
StripeResource,
StripeTransientError,
+ StripeValidationError,
_all_known_webhook_events,
_coerce_incremental_cursor,
_is_non_list_stripe_response,
@@ -494,6 +495,34 @@ class TestStripeSource:
assert "one of our backends" not in message
assert "validation failed" not in message.lower()
+ @pytest.mark.parametrize(
+ "stripe_error,expected_start",
+ [
+ ("Invalid request (check your request parameters)", "Stripe couldn't read your account"),
+ ("This API call cannot be made with a publishable API key.", "Your Stripe API key is a publishable key"),
+ ],
+ )
+ def test_validate_credentials_validation_error_hides_raw_stripe_text(self, stripe_error, expected_start):
+ config = StripeSourceConfig(
+ auth_method=StripeAuthMethodConfig(selection="api_key", stripe_secret_key="rk_live_x")
+ )
+
+ with (
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.stripe.source.validate_stripe_credentials",
+ side_effect=StripeValidationError({"Customer": stripe_error}),
+ ),
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.stripe.source.capture_exception"
+ ),
+ ):
+ ok, message = self.source.validate_credentials(config, team_id=1)
+
+ assert ok is False
+ assert message is not None
+ assert message.startswith(expected_start)
+ assert stripe_error not in message
+
@pytest.mark.parametrize(
"body,expected",
[