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(instagram): say what to do when the credential probe fails (#99004)

Tom Owers234b03d6275e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py
index d6bb37317737..7b8a7dbf675e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py
@@ -10,6 +10,8 @@ from structlog.types import FilteringBoundLogger
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
from urllib3.util.retry import Retry
+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
@@ -688,8 +690,21 @@ def validate_credentials(
"The Instagram connection is missing permissions this source needs. Reconnect it and grant "
"access to your page, Instagram insights and comments."
)
- except Exception:
- return False, "Could not reach the Instagram API with this connection."
+ except (InstagramRetryableError, InstagramRequestBudgetError):
+ return False, "Instagram is busy or temporarily unavailable. Wait a few minutes and try again."
+ except InstagramBadRequestError:
+ return False, (
+ "Instagram rejected the request for that account. Check the account is an Instagram "
+ "professional account linked to your Facebook page, then try again."
+ )
+ except Exception as e:
+ # Anything left is a transport failure or a bug on our side. The probe returns a message
+ # rather than raising, so nothing above the source would record it otherwise.
+ capture_exception(e)
+ return (
+ False,
+ "PostHog couldn't check this Instagram connection. Reconnect your Instagram account and try again.",
+ )
if not body.get("id"):
return False, "That account is not an Instagram professional account. Pick a different account."
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py
index f7e817d569f6..96c6d3d98330 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py
@@ -835,6 +835,32 @@ class TestValidateCredentials:
assert is_valid is False
assert message is not None and expected_fragment in message
+ @pytest.mark.parametrize(
+ "error,expected_fragment",
+ [
+ (InstagramRetryableError("Instagram API error (retryable): status=503"), "temporarily unavailable"),
+ (
+ InstagramRequestBudgetError("Instagram API request budget of 1 requests spent"),
+ "temporarily unavailable",
+ ),
+ (InstagramBadRequestError("Instagram API error: status=400, code=100"), "professional account"),
+ (RuntimeError("boom"), "Reconnect your Instagram account"),
+ ],
+ )
+ def test_remaining_failures_get_actionable_messages(self, error: Exception, expected_fragment: str) -> None:
+ # These all used to collapse into one message that named no next step, and the unexpected
+ # case was swallowed without being recorded anywhere.
+ with (
+ mock.patch.object(InstagramClient, "get", side_effect=error),
+ mock.patch(f"{MODULE}.capture_exception") as capture,
+ ):
+ is_valid, message = validate_credentials("tok", "v23.0", LOGGER, instagram_account_id=ACCOUNT_ID)
+
+ assert is_valid is False
+ assert message is not None and expected_fragment in message
+ assert str(error) not in message
+ assert capture.called is isinstance(error, RuntimeError)
+
def test_a_node_without_an_id_is_not_a_professional_account(self) -> None:
session = FakeSession([(ACCOUNT_ID, FakeResponse(200, {}))])
with mock.patch(f"{MODULE}.make_tracked_session", return_value=session):