: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(query): show malformed csv rows as a user error (#118542)
posthog[bot]c639a0a78e7a
diff --git a/posthog/errors.py b/posthog/errors.pyindex 556d91a4a970..9f88510dcdf2 100644--- a/posthog/errors.py+++ b/posthog/errors.py@@ -86,6 +86,7 @@ def clickhouse_error_type(e: Exception) -> str:STORAGE_FILE_URI_PATTERN = re.compile(r"\(in file/uri ([^)]+)\)")+CSV_ROW_NUMBER_PATTERN = re.compile(r"\(at row (\d+)\)")_ACRONYM_BOUNDARY = re.compile(r"([A-Z]+)([A-Z][a-z])")_WORD_BOUNDARY = re.compile(r"([a-z0-9])([A-Z])")BAD_GET_TYPE_MISMATCH_PATTERN = re.compile(@@ -114,6 +115,20 @@ def _wrap_storage_file_changed_error(err: ServerException) -> "CHQueryErrorS3Fil)+def _wrap_malformed_csv_row_error(err: ServerException) -> "CHQueryErrorMalformedCsvRow":+ row_match = CSV_ROW_NUMBER_PATTERN.search(err.message)+ file_uris = STORAGE_FILE_URI_PATTERN.findall(err.message)+ location = f" at row {row_match.group(1)}" if row_match else ""+ if file_uris:+ location += f" of {file_uris[-1]}"+ return CHQueryErrorMalformedCsvRow(+ f"A row in a CSV file backing this table is malformed{location}. "+ "Check that row for unescaped quotes, a wrong delimiter, or a different number of columns.",+ code=err.code,+ code_name="malformed_csv_row",+ )++def _wrap_bad_get_error(err: ServerException) -> "CHQueryErrorBadGet":match = BAD_GET_TYPE_MISMATCH_PATTERN.search(err.message)message = BAD_GET_FALLBACK_MESSAGE@@ -199,6 +214,10 @@ def wrap_clickhouse_query_error(err: Exception) -> Exception:return CHQueryErrorS3Error(f"S3 error occurred. ({err.message})", code=err.code)elif name == "INCORRECT_DATA" and "Not a Parquet file" in err.message and "(in file/uri" in err.message:return _wrap_storage_file_changed_error(err)+ elif name == "INCORRECT_DATA" and "Expected end of line" in err.message:+ # A bad row in a self-managed CSV file. Table validation reads only the first rows, so a later+ # bad row first shows up at query time. The raw message holds row contents, so it stays hidden.+ return _wrap_malformed_csv_row_error(err)elif name == "STD_EXCEPTION" and "deserialize thrift" in err.message:# A Parquet file with corrupted or oversized thrift metadata (e.g.# "Couldn't deserialize thrift: TProtocolException: Exceeded size limit").@@ -317,6 +336,12 @@ class CHQueryErrorCorruptedParquetMetadata(ExposedCHQueryError):pass+class CHQueryErrorMalformedCsvRow(ExposedCHQueryError):+ """A row in a CSV file backing a warehouse table has the wrong shape."""++ pass++# User query errors - these are errors caused by user input/queriesclass CHQueryErrorIllegalTypeOfArgument(ExposedCHQueryError):passdiff --git a/posthog/test/test_errors.py b/posthog/test/test_errors.pyindex e1e6be2baeb6..ccfee35cbe55 100644--- a/posthog/test/test_errors.py+++ b/posthog/test/test_errors.py@@ -7,6 +7,7 @@ from posthog.errors import (CHQueryErrorCannotParseBool,CHQueryErrorCannotParseUuid,CHQueryErrorInvalidJoinOnExpression,+ CHQueryErrorMalformedCsvRow,CHQueryErrorUnknownType,ExposedCHQueryError,InternalCHQueryError,@@ -226,6 +227,43 @@ class TestWrapClickhouseQueryError:assert str(wrapped) == BAD_GET_FALLBACK_MESSAGEassert "private" not in wrapped.message+ @parameterized.expand(+ [+ (+ "with_row_and_file",+ "DB::Exception: Expected end of line: (at row 115622)\n: Row 115621: private-row-value "+ "(in file/uri example-bucket/data/export.csv): While executing CSVRowInputFormat. "+ "Stack trace: private-stack-frame",+ "A row in a CSV file backing this table is malformed at row 115622 of example-bucket/data/export.csv. "+ "Check that row for unescaped quotes, a wrong delimiter, or a different number of columns.",+ ),+ (+ "with_file_uri_text_in_row",+ "DB::Exception: Expected end of line: (at row 7)\n: Row 6: (in file/uri private-row-value) "+ "(in file/uri example-bucket/data/export.csv): While executing CSVRowInputFormat.",+ "A row in a CSV file backing this table is malformed at row 7 of example-bucket/data/export.csv. "+ "Check that row for unescaped quotes, a wrong delimiter, or a different number of columns.",+ ),+ (+ "without_location",+ "DB::Exception: Expected end of line: private-row-value",+ "A row in a CSV file backing this table is malformed. "+ "Check that row for unescaped quotes, a wrong delimiter, or a different number of columns.",+ ),+ ]+ )+ def test_malformed_csv_row_is_exposed_without_row_contents(self, _name: str, raw: str, expected: str) -> None:+ wrapped = wrap_clickhouse_query_error(+ ServerException(raw, code=117, nested=ServerException("fake-nested-secret", code=1000))+ )++ assert isinstance(wrapped, CHQueryErrorMalformedCsvRow)+ assert str(wrapped) == expected+ assert "private" not in wrapped.message+ assert wrapped.code == 117+ assert wrapped.code_name == "malformed_csv_row"+ assert wrapped.nested is None+@parameterized.expand([# NETWORK_ERROR (210) is a genuine server-side fault and must not be exposed.