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(vitally): surface real HTTP error from Messages sync instead of KeyError (#63738)

Tom Owerscd12997545ed
diff --git a/posthog/temporal/data_imports/sources/vitally/tests/test_vitally.py b/posthog/temporal/data_imports/sources/vitally/tests/test_vitally.py
index 3ba5a9ac47ce..4f5be6146da7 100644
--- a/posthog/temporal/data_imports/sources/vitally/tests/test_vitally.py
+++ b/posthog/temporal/data_imports/sources/vitally/tests/test_vitally.py
@@ -3,11 +3,14 @@ from typing import Any, cast
import pytest
from unittest.mock import MagicMock, Mock, patch
+from requests import HTTPError
+
from posthog.temporal.data_imports.sources.vitally.settings import CUSTOM_OBJECT_SCHEMA_PREFIX
from posthog.temporal.data_imports.sources.vitally.source import VitallySource
from posthog.temporal.data_imports.sources.vitally.vitally import (
VitallyPaginator,
get_custom_object_records_resource,
+ get_messages,
list_custom_object_definitions,
vitally_source,
)
@@ -119,6 +122,27 @@ class TestListCustomObjectDefinitions:
list_custom_object_definitions("secret", "EU", None)
+class TestGetMessages:
+ @patch("posthog.temporal.data_imports.sources.vitally.vitally.make_tracked_session")
+ def test_raises_http_error_instead_of_keyerror_when_conversations_request_fails(self, mock_session_factory):
+ # A non-2xx conversations list response carries an error body with no "results" key.
+ # Previously get_messages indexed json["results"] directly, surfacing a misleading
+ # KeyError('results'); now raise_for_status lets the real HTTP error through.
+ bad = _make_response({"message": "Unauthorized"}, status_code=401)
+ bad.raise_for_status.side_effect = HTTPError("401 Client Error: Unauthorized", response=bad)
+ mock_session_factory.return_value = _make_session([bad])
+
+ with pytest.raises(HTTPError, match="401"):
+ list(get_messages("secret", "EU", None, None, False, MagicMock()))
+
+ @patch("posthog.temporal.data_imports.sources.vitally.vitally.make_tracked_session")
+ def test_handles_missing_results_key_without_keyerror(self, mock_session_factory):
+ # Defensive: a 200 response that omits "results" must end pagination cleanly, not KeyError.
+ mock_session_factory.return_value = _make_session([_make_response({"next": None})])
+
+ assert list(get_messages("secret", "EU", None, None, False, MagicMock())) == []
+
+
class TestGetCustomObjectRecordsResource:
@pytest.mark.parametrize("should_use_incremental_field", [False, True])
def test_builds_resource(self, should_use_incremental_field):
diff --git a/posthog/temporal/data_imports/sources/vitally/vitally.py b/posthog/temporal/data_imports/sources/vitally/vitally.py
index 02c193f6d550..13a0fdd079b3 100644
--- a/posthog/temporal/data_imports/sources/vitally/vitally.py
+++ b/posthog/temporal/data_imports/sources/vitally/vitally.py
@@ -433,8 +433,8 @@ def get_messages(
response = session.send(prepared_request)
logger.debug(f"Requesting {prepared_request.url}")
- json = response.json()
- results = json["results"]
+ response.raise_for_status()
+ results = response.json().get("results") or []
for conversation in results:
id = conversation.get("id")