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(convex): surface the error code on an unclassified schema-discovery 400 (#127999)

Tom Owers311534c0c020
diff --git a/products/warehouse_sources/sources/convex/convex.py b/products/warehouse_sources/sources/convex/convex.py
index 4fbd31295cf7..85f01367e7cc 100644
--- a/products/warehouse_sources/sources/convex/convex.py
+++ b/products/warehouse_sources/sources/convex/convex.py
@@ -181,6 +181,16 @@ class StreamingExportNotEnabledError(Exception):
pass
+class ConvexApiError(HTTPError):
+ """A structured 400 from Convex whose error code this module doesn't special-case.
+
+ `raise_for_status()` alone produces a body-less "400 Client Error: Bad Request for url: ..."
+ that can never be told apart from any other 400, so the actual Convex code and message
+ (always present on a structured error response) are folded into the message here instead of
+ discarded - the same reasoning that motivated surfacing StreamingExportNotEnabled below.
+ """
+
+
def get_json_schemas(deploy_url: str, deploy_key: str) -> dict[str, Any]:
url = f"{deploy_url.rstrip('/')}/api/json_schemas"
# byComponent=true groups the response by component so non-default components are discoverable.
@@ -192,13 +202,21 @@ def get_json_schemas(deploy_url: str, deploy_key: str) -> dict[str, Any]:
error_data = response.json()
except ValueError:
error_data = {}
+ # A 400 body can parse as valid JSON that isn't an object (e.g. a bare string or array),
+ # which has no .get - treat it the same as an unparseable body rather than crashing.
+ if not isinstance(error_data, dict):
+ error_data = {}
# A plain HTTPError's message never carries the response body, so without this the
# StreamingExportNotEnabled entry in get_non_retryable_errors can never match here -
# it only matched by accident via validate_credentials' own body parsing below.
- if error_data.get("code") == "StreamingExportNotEnabled":
+ code = error_data.get("code")
+ if code == "StreamingExportNotEnabled":
raise StreamingExportNotEnabledError(
"StreamingExportNotEnabled: streaming export requires the Convex Professional plan."
)
+ message = error_data.get("message") or ""
+ if code or message:
+ raise ConvexApiError(f"{code}: {message}".rstrip(": ") if code else message, response=response)
response.raise_for_status()
return response.json()
diff --git a/products/warehouse_sources/sources/convex/tests/test_convex.py b/products/warehouse_sources/sources/convex/tests/test_convex.py
index 7e4d41596ef1..97127a878253 100644
--- a/products/warehouse_sources/sources/convex/tests/test_convex.py
+++ b/products/warehouse_sources/sources/convex/tests/test_convex.py
@@ -18,6 +18,7 @@ from requests.exceptions import (
from products.warehouse_sources.sources.convex._config import ConvexSourceConfig
from products.warehouse_sources.sources.convex.convex import (
_CONVEX_RETRY,
+ ConvexApiError,
ConvexDataSyncCursor,
ConvexResumeConfig,
ConvexResyncRequiredError,
@@ -304,6 +305,38 @@ class TestConvexNonRetryableErrors:
with scripted_network([ScriptedResponse(status=400, body=b"not JSON")]), pytest.raises(HTTPError):
get_json_schemas("https://x.convex.cloud", "key")
+ @parameterized.expand(
+ [
+ # A structured 400 whose code isn't StreamingExportNotEnabled used to fall straight
+ # through to raise_for_status(), producing a body-less "400 Client Error: Bad Request
+ # for url: ..." that discards the one piece of information that would let this error
+ # ever be diagnosed or classified. It must surface instead, as StreamingExportNotEnabled
+ # already does.
+ ("code_and_message", {"code": "SomeOtherCode", "message": "nope"}, "SomeOtherCode: nope"),
+ # A code with no message must not render the message's .get default as the literal word
+ # "None" (error_data.get("message", "") only applies when the key is missing entirely).
+ ("code_only", {"code": "SomeOtherCode"}, "SomeOtherCode"),
+ ("code_with_null_message", {"code": "SomeOtherCode", "message": None}, "SomeOtherCode"),
+ # A message with no code is the same discarded-information bug for the other half of the
+ # body - it must surface too, not just the code half.
+ ("message_only", {"message": "nope, no code"}, "nope, no code"),
+ ]
+ )
+ def test_get_json_schemas_400_with_other_code_surfaces_it_instead_of_a_bare_http_error(
+ self, _name: str, body: dict[str, Any], expected: str
+ ) -> None:
+ with (
+ scripted_network([ScriptedResponse(status=400, json=body)]),
+ pytest.raises(ConvexApiError, match=expected),
+ ):
+ get_json_schemas("https://x.convex.cloud", "key")
+
+ def test_get_json_schemas_400_with_non_object_json_body_falls_through_to_http_error(self) -> None:
+ # A 400 body can be valid JSON that isn't an object (e.g. a bare string), which has no
+ # .get - this must not crash with an AttributeError, just like an unparseable body.
+ with scripted_network([ScriptedResponse(status=400, json="Bad Request")]), pytest.raises(HTTPError):
+ get_json_schemas("https://x.convex.cloud", "key")
+
@parameterized.expand(
[
("401", "401 Client Error: Unauthorized for url: https://x.convex.cloud/api/v1/data/sync"),