: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(errors): mark user-error ClickHouse query codes user-safe (#78521)
Thiago Salvatore81cfdda0afe9
diff --git a/posthog/errors.py b/posthog/errors.pyindex c1901a2b04c9..5ee931dad660 100644--- a/posthog/errors.py+++ b/posthog/errors.py@@ -352,9 +352,9 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {2: ErrorCodeMeta("UNSUPPORTED_PARAMETER"),3: ErrorCodeMeta("UNEXPECTED_END_OF_FILE"),4: ErrorCodeMeta("EXPECTED_END_OF_FILE"),- 6: ErrorCodeMeta(- "CANNOT_PARSE_TEXT", category=QueryErrorCategory.USER_ERROR- ), # failed to parse value from text representation+ # Stays internal: the CH message embeds the failing data value, which would leak stored+ # data to anonymous viewers of public shared insights. Only user_safe once sanitized.+ 6: ErrorCodeMeta("CANNOT_PARSE_TEXT", category=QueryErrorCategory.USER_ERROR),7: ErrorCodeMeta("INCORRECT_NUMBER_OF_COLUMNS"),8: ErrorCodeMeta("THERE_IS_NO_COLUMN"),9: ErrorCodeMeta("SIZES_OF_COLUMNS_DOESNT_MATCH"),@@ -385,13 +385,13 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {42: ErrorCodeMeta("NUMBER_OF_ARGUMENTS_DOESNT_MATCH", user_safe=True),43: ErrorCodeMeta("ILLEGAL_TYPE_OF_ARGUMENT", user_safe=True),44: ErrorCodeMeta(- "ILLEGAL_COLUMN", category=QueryErrorCategory.USER_ERROR+ "ILLEGAL_COLUMN", user_safe=True), # column has wrong type for the operation (e.g. non-constant where constant required)46: ErrorCodeMeta("UNKNOWN_FUNCTION", user_safe=True),47: ErrorCodeMeta("UNKNOWN_IDENTIFIER", user_safe=True), # TODO: Unset user_safe once HogQL is accurate in Data WH48: ErrorCodeMeta("NOT_IMPLEMENTED"),49: ErrorCodeMeta("LOGICAL_ERROR"),- 50: ErrorCodeMeta("UNKNOWN_TYPE", category=QueryErrorCategory.USER_ERROR), # referenced data type does not exist+ 50: ErrorCodeMeta("UNKNOWN_TYPE", user_safe=True), # referenced data type does not exist51: ErrorCodeMeta("EMPTY_LIST_OF_COLUMNS_QUERIED"),52: ErrorCodeMeta("COLUMN_QUERIED_MORE_THAN_ONCE"),53: ErrorCodeMeta("TYPE_MISMATCH", user_safe=True),@@ -400,13 +400,17 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {57: ErrorCodeMeta("TABLE_ALREADY_EXISTS"),58: ErrorCodeMeta("TABLE_METADATA_ALREADY_EXISTS"),59: ErrorCodeMeta(- "ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER", category=QueryErrorCategory.USER_ERROR+ "ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER", user_safe=True), # WHERE/HAVING column is not boolean-convertible60: ErrorCodeMeta("UNKNOWN_TABLE", user_safe=True),+ # Stays internal: HogQL validates syntax before ClickHouse, so a raw CH syntax error means+ # PostHog generated invalid SQL — a bug we want in error tracking, not hidden as user-safe.62: ErrorCodeMeta("SYNTAX_ERROR", category=QueryErrorCategory.USER_ERROR),63: ErrorCodeMeta("UNKNOWN_AGGREGATE_FUNCTION", user_safe=True),68: ErrorCodeMeta("CANNOT_GET_SIZE_OF_FIELD"),- 69: ErrorCodeMeta("ARGUMENT_OUT_OF_BOUND", category=QueryErrorCategory.USER_ERROR),+ # Fixed message: the raw CH text formats a per-row value (e.g. geoToH3 resolution) into the error.+ 69: ErrorCodeMeta("ARGUMENT_OUT_OF_BOUND", user_safe="An argument is out of bounds."),+ # 70/72 stay internal: their CH messages embed the failing data value (see code 6 note).70: ErrorCodeMeta("CANNOT_CONVERT_TYPE", category=QueryErrorCategory.USER_ERROR),71: ErrorCodeMeta("CANNOT_WRITE_AFTER_END_OF_BUFFER"),72: ErrorCodeMeta("CANNOT_PARSE_NUMBER", category=QueryErrorCategory.USER_ERROR),@@ -417,9 +421,7 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {77: ErrorCodeMeta("CANNOT_CLOSE_FILE"),78: ErrorCodeMeta("UNKNOWN_TYPE_OF_QUERY"),79: ErrorCodeMeta("INCORRECT_FILE_NAME"),- 80: ErrorCodeMeta(- "INCORRECT_QUERY", category=QueryErrorCategory.USER_ERROR- ), # query parses but is semantically invalid+ 80: ErrorCodeMeta("INCORRECT_QUERY", user_safe=True), # query parses but is semantically invalid81: ErrorCodeMeta("UNKNOWN_DATABASE"),82: ErrorCodeMeta("DATABASE_ALREADY_EXISTS"),83: ErrorCodeMeta("DIRECTORY_DOESNT_EXIST"),@@ -455,9 +457,7 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {119: ErrorCodeMeta("ENGINE_REQUIRED"),120: ErrorCodeMeta("CANNOT_INSERT_VALUE_OF_DIFFERENT_SIZE_INTO_TUPLE"),121: ErrorCodeMeta("UNSUPPORTED_JOIN_KEYS"),- 122: ErrorCodeMeta(- "INCOMPATIBLE_COLUMNS", category=QueryErrorCategory.USER_ERROR- ), # column types don't match expected schema+ 122: ErrorCodeMeta("INCOMPATIBLE_COLUMNS", user_safe=True), # column types don't match expected schema123: ErrorCodeMeta("UNKNOWN_TYPE_OF_AST_NODE"),124: ErrorCodeMeta("INCORRECT_ELEMENT_OF_SET"),125: ErrorCodeMeta("INCORRECT_RESULT_OF_SCALAR_SUBQUERY"),@@ -496,7 +496,7 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {170: ErrorCodeMeta("BAD_GET"),172: ErrorCodeMeta("CANNOT_CREATE_DIRECTORY"),173: ErrorCodeMeta("CANNOT_ALLOCATE_MEMORY", category=QueryErrorCategory.QUERY_PERFORMANCE_ERROR),- 174: ErrorCodeMeta("CYCLIC_ALIASES", category=QueryErrorCategory.USER_ERROR),+ 174: ErrorCodeMeta("CYCLIC_ALIASES", user_safe=True),179: ErrorCodeMeta("MULTIPLE_EXPRESSIONS_FOR_ALIAS"),180: ErrorCodeMeta("THERE_IS_NO_PROFILE"),181: ErrorCodeMeta("ILLEGAL_FINAL"),@@ -523,7 +523,7 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {208: ErrorCodeMeta("EMPTY_NESTED_TABLE"),209: ErrorCodeMeta("SOCKET_TIMEOUT"),210: ErrorCodeMeta("NETWORK_ERROR"),- 211: ErrorCodeMeta("EMPTY_QUERY", category=QueryErrorCategory.USER_ERROR),+ 211: ErrorCodeMeta("EMPTY_QUERY", user_safe=True),212: ErrorCodeMeta("UNKNOWN_LOAD_BALANCING"),213: ErrorCodeMeta("UNKNOWN_TOTALS_MODE"),214: ErrorCodeMeta("CANNOT_STATVFS"),@@ -579,7 +579,7 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {262: ErrorCodeMeta("BAD_COLLATION"),263: ErrorCodeMeta("CANNOT_COMPILE_CODE"),264: ErrorCodeMeta(- "INCOMPATIBLE_TYPE_OF_JOIN", category=QueryErrorCategory.USER_ERROR+ "INCOMPATIBLE_TYPE_OF_JOIN", user_safe=True), # join type not supported for this engine or context265: ErrorCodeMeta("NO_AVAILABLE_REPLICA"),266: ErrorCodeMeta("MISMATCH_REPLICAS_DATA_SOURCES"),@@ -655,7 +655,7 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {374: ErrorCodeMeta("INVALID_SESSION_TIMEOUT"),375: ErrorCodeMeta("CANNOT_DLOPEN"),376: ErrorCodeMeta("CANNOT_PARSE_UUID", user_safe=True),- 377: ErrorCodeMeta("ILLEGAL_SYNTAX_FOR_DATA_TYPE", category=QueryErrorCategory.USER_ERROR),+ 377: ErrorCodeMeta("ILLEGAL_SYNTAX_FOR_DATA_TYPE", user_safe=True),378: ErrorCodeMeta("DATA_TYPE_CANNOT_HAVE_ARGUMENTS"),380: ErrorCodeMeta("CANNOT_KILL"),381: ErrorCodeMeta("HTTP_LENGTH_REQUIRED"),@@ -685,7 +685,8 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {403: ErrorCodeMeta("INVALID_JOIN_ON_EXPRESSION", category=QueryErrorCategory.USER_ERROR),404: ErrorCodeMeta("BAD_ODBC_CONNECTION_STRING"),406: ErrorCodeMeta("TOP_AND_LIMIT_TOGETHER"),- 407: ErrorCodeMeta("DECIMAL_OVERFLOW", category=QueryErrorCategory.USER_ERROR),+ # Fixed message: the raw CH text can format a converted decimal value into the overflow error.+ 407: ErrorCodeMeta("DECIMAL_OVERFLOW", user_safe="Decimal overflow while executing query."),408: ErrorCodeMeta("BAD_REQUEST_PARAMETER"),410: ErrorCodeMeta("EXTERNAL_SERVER_IS_NOT_RESPONDING"),411: ErrorCodeMeta("PTHREAD_ERROR"),@@ -929,15 +930,15 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {672: ErrorCodeMeta("INVALID_SCHEDULER_NODE"),673: ErrorCodeMeta("RESOURCE_ACCESS_DENIED"),674: ErrorCodeMeta("RESOURCE_NOT_FOUND"),+ # IP parse errors stay internal: their CH messages embed the failing data value (see code 6 note).675: ErrorCodeMeta("CANNOT_PARSE_IPV4", category=QueryErrorCategory.USER_ERROR),676: ErrorCodeMeta("CANNOT_PARSE_IPV6", category=QueryErrorCategory.USER_ERROR),677: ErrorCodeMeta("THREAD_WAS_CANCELED"),678: ErrorCodeMeta("IO_URING_INIT_FAILED"),679: ErrorCodeMeta("IO_URING_SUBMIT_ERROR"),690: ErrorCodeMeta("MIXED_ACCESS_PARAMETER_TYPES"),- 691: ErrorCodeMeta(- "UNKNOWN_ELEMENT_OF_ENUM", category=QueryErrorCategory.USER_ERROR- ), # value not found in enum definition+ # Stays internal: the CH message embeds the offending enum value (see code 6 note).+ 691: ErrorCodeMeta("UNKNOWN_ELEMENT_OF_ENUM", category=QueryErrorCategory.USER_ERROR),692: ErrorCodeMeta("TOO_MANY_MUTATIONS"),693: ErrorCodeMeta("AWS_ERROR"),694: ErrorCodeMeta("ASYNC_LOAD_CYCLE"),@@ -949,9 +950,7 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {700: ErrorCodeMeta("USER_SESSION_LIMIT_EXCEEDED"),701: ErrorCodeMeta("CLUSTER_DOESNT_EXIST"),702: ErrorCodeMeta("CLIENT_INFO_DOES_NOT_MATCH"),- 703: ErrorCodeMeta(- "INVALID_IDENTIFIER", category=QueryErrorCategory.USER_ERROR- ), # identifier contains invalid characters+ 703: ErrorCodeMeta("INVALID_IDENTIFIER", user_safe=True), # identifier contains invalid characters704: ErrorCodeMeta("QUERY_CACHE_USED_WITH_NONDETERMINISTIC_FUNCTIONS"),705: ErrorCodeMeta("TABLE_NOT_EMPTY"),706: ErrorCodeMeta("LIBSSH_ERROR"),diff --git a/posthog/test/test_errors.py b/posthog/test/test_errors.pyindex 40d777ef0856..0e3fe90610e0 100644--- a/posthog/test/test_errors.py+++ b/posthog/test/test_errors.py@@ -13,13 +13,24 @@ from posthog.errors import (class TestWrapClickhouseQueryError:@parameterized.expand([- # Ambiguous-identifier query errors are the user's malformed HogQL, not a server fault,- # so they must wrap as ExposedCHQueryError and stay out of error tracking.+ # These reveal only the user's own query or schema, not stored data values, so they wrap+ # as ExposedCHQueryError and expose the real ClickHouse message. Reverting any to+ # non-user_safe reintroduces the generic "ClickHouse error while executing query." message.+ (44, "ILLEGAL_COLUMN"),+ (50, "UNKNOWN_TYPE"),+ (59, "ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER"),+ (80, "INCORRECT_QUERY"),+ (122, "INCOMPATIBLE_COLUMNS"),+ (174, "CYCLIC_ALIASES"),(207, "AMBIGUOUS_IDENTIFIER"),+ (211, "EMPTY_QUERY"),+ (264, "INCOMPATIBLE_TYPE_OF_JOIN"),(352, "AMBIGUOUS_COLUMN_NAME"),+ (377, "ILLEGAL_SYNTAX_FOR_DATA_TYPE"),+ (703, "INVALID_IDENTIFIER"),])- def test_ambiguous_identifier_codes_wrap_as_exposed_error(self, code: int, name: str) -> None:+ def test_user_error_codes_wrap_as_exposed_error(self, code: int, name: str) -> None:err = ServerException(f"DB::Exception: {name}", code=code)wrapped = wrap_clickhouse_query_error(err)@@ -27,9 +38,42 @@ class TestWrapClickhouseQueryError:assert isinstance(wrapped, ExposedCHQueryError)assert look_up_clickhouse_error_code_meta(err).get_category() == QueryErrorCategory.USER_ERROR- def test_unmapped_internal_code_stays_internal(self) -> None:- # NETWORK_ERROR (210) is a genuine server-side fault and must not be exposed.- err = ServerException("DB::Exception: NETWORK_ERROR", code=210)+ @parameterized.expand(+ [+ # These are exposed but their raw CH message embeds a per-row data value, so they carry a+ # fixed user_safe string. The assertion guards against a revert to user_safe=True, which+ # would pass the raw ClickHouse text (and the source value) straight through.+ (69, "ARGUMENT_OUT_OF_BOUND", "An argument is out of bounds."),+ (407, "DECIMAL_OVERFLOW", "Decimal overflow while executing query."),+ ]+ )+ def test_fixed_message_codes_hide_raw_clickhouse_text(self, code: int, name: str, message: str) -> None:+ err = ServerException(f"DB::Exception: {name} leaked_value=42", code=code)++ wrapped = wrap_clickhouse_query_error(err)++ assert isinstance(wrapped, ExposedCHQueryError)+ assert str(wrapped) == message++ @parameterized.expand(+ [+ # NETWORK_ERROR (210) is a genuine server-side fault and must not be exposed.+ (210, "NETWORK_ERROR"),+ # SYNTAX_ERROR (62) stays internal: HogQL validates syntax first, so a raw CH syntax error+ # signals a PostHog SQL-generation bug that belongs in error tracking.+ (62, "SYNTAX_ERROR"),+ # These parse/convert codes embed the failing data value in the CH message, so they stay+ # internal to avoid leaking source values on public shared insights.+ (6, "CANNOT_PARSE_TEXT"),+ (70, "CANNOT_CONVERT_TYPE"),+ (72, "CANNOT_PARSE_NUMBER"),+ (675, "CANNOT_PARSE_IPV4"),+ (676, "CANNOT_PARSE_IPV6"),+ (691, "UNKNOWN_ELEMENT_OF_ENUM"),+ ]+ )+ def test_codes_stay_internal(self, code: int, name: str) -> None:+ err = ServerException(f"DB::Exception: {name}", code=code)wrapped = wrap_clickhouse_query_error(err)