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(sql): explain more query errors without exposing raw details (#109981)

Marius Andra662f459147d2
diff --git a/docs/published/handbook/engineering/databases/hogql-python.md b/docs/published/handbook/engineering/databases/hogql-python.md
index 2f49d2c21187..9a95cb348035 100644
--- a/docs/published/handbook/engineering/databases/hogql-python.md
+++ b/docs/published/handbook/engineering/databases/hogql-python.md
@@ -65,6 +65,18 @@ Regex matching uses RE2 syntax, so backreferences and lookaround are unsupported
SQL LIKE and ILIKE patterns sent to ClickHouse are not subject to these VM limits.
For non-nullable materialized columns, patterns above 16,384 characters skip the optional sentinel-based rewrite and use the normal property read.
+## ClickHouse query errors
+
+`posthog/errors.py` maps ClickHouse error codes to exceptions. Query APIs expose `ExposedCHQueryError` messages and hide `InternalCHQueryError` messages.
+For additional user-correctable errors, set `ErrorCodeMeta.user_safe` to a fixed explanation with a next step.
+Parsing, array, regular expression, scalar subquery, JOIN, and LIMIT errors use these explanations where raw details cannot be exposed safely.
+Unrecognized error codes stay internal.
+
+Do not assume an error code makes its raw message safe. ClickHouse can append expressions and query context after the initial exception.
+Those messages can contain storage credentials, signed URLs, settings, or source data values, including values that a shared insight does not otherwise reveal.
+Check both the throw sites and the exception enrichment paths before allowing raw text.
+Keep importable exception classes when adding a fixed explanation, and test the wrapped message as well as its string representation.
+
## AST nodes
If you want more control, you can build the AST nodes directly. The same query above can be written as:
diff --git a/ee/clickhouse/test/test_error.py b/ee/clickhouse/test/test_error.py
index bd5cdfd523d2..66527599cce9 100644
--- a/ee/clickhouse/test/test_error.py
+++ b/ee/clickhouse/test/test_error.py
@@ -129,7 +129,7 @@ from posthog.exceptions import ClickHouseClusterMemoryLimitExceeded, ClickHouseQ
code=467,
),
"CHQueryErrorCannotParseBool",
- "Cannot parse boolean value here: 'null', should be 'true' or 'false' controlled by setting bool_true_representation and bool_false_representation: while converting 'null' to Bool.",
+ "Cannot parse a boolean. Check for values other than true, false, 1, or 0.",
467,
"CHQueryErrorCannotParseBool",
),
@@ -139,7 +139,7 @@ from posthog.exceptions import ClickHouseClusterMemoryLimitExceeded, ClickHouseQ
code=376,
),
"CHQueryErrorCannotParseUuid",
- "Cannot parse uuid 2026072018044213140: while converting '2026072018044213140' to UUID: while executing function equals.",
+ "Cannot parse a UUID. Check the format of the values converted to UUIDs.",
376,
"CHQueryErrorCannotParseUuid",
),
diff --git a/posthog/errors.py b/posthog/errors.py
index 784507617301..2126bbf0b73f 100644
--- a/posthog/errors.py
+++ b/posthog/errors.py
@@ -113,6 +113,7 @@ def wrap_clickhouse_query_error(err: Exception) -> Exception:
meta = look_up_clickhouse_error_code_meta(err)
name = meta.name
+ message = meta.user_safe if isinstance(meta.user_safe, str) else err.message
# Naming convention:
# - Exceptions starting with ClickHouse inherit from APIException and are not sent to error reporting.
@@ -191,13 +192,13 @@ def wrap_clickhouse_query_error(err: Exception) -> Exception:
elif name == "TOO_MANY_BYTES":
return CHQueryErrorTooManyBytes(err.message, code=err.code, code_name="too_many_bytes")
elif name == "CANNOT_PARSE_UUID":
- return CHQueryErrorCannotParseUuid(err.message, code=err.code, code_name="cannot_parse_uuid")
+ return CHQueryErrorCannotParseUuid(message, code=err.code, code_name="cannot_parse_uuid")
elif name == "CANNOT_PARSE_BOOL":
- return CHQueryErrorCannotParseBool(err.message, code=err.code, code_name="cannot_parse_bool")
+ return CHQueryErrorCannotParseBool(message, code=err.code, code_name="cannot_parse_bool")
elif name == "UNSUPPORTED_METHOD":
return CHQueryErrorUnsupportedMethod(err.message, code=err.code, code_name="unsupported_method")
elif name == "INVALID_JOIN_ON_EXPRESSION":
- return CHQueryErrorInvalidJoinOnExpression(err.message, code=err.code, code_name="invalid_join_on_expression")
+ return CHQueryErrorInvalidJoinOnExpression(message, code=err.code, code_name="invalid_join_on_expression")
elif name == "UNKNOWN_TABLE":
return CHQueryErrorUnknownTable(err.message, code=err.code, code_name="unknown_table")
@@ -205,7 +206,6 @@ def wrap_clickhouse_query_error(err: Exception) -> Exception:
else:
name = f"CHQueryError{meta.label}"
processed_error_class = ExposedCHQueryError if meta.user_safe else InternalCHQueryError
- message = meta.user_safe if isinstance(meta.user_safe, str) else err.message
return type(name, (processed_error_class,), {})(message, code=err.code, code_name=meta.name.lower())
@@ -318,7 +318,7 @@ class CHQueryErrorUnsupportedMethod(InternalCHQueryError):
pass
-class CHQueryErrorInvalidJoinOnExpression(InternalCHQueryError):
+class CHQueryErrorInvalidJoinOnExpression(ExposedCHQueryError):
pass
@@ -363,15 +363,18 @@ class CHQueryErrorUnknownTable(ExposedCHQueryError):
# Remember to add back the `user_safe` args though!
CLICKHOUSE_UNKNOWN_EXCEPTION = ErrorCodeMeta("UNKNOWN_EXCEPTION")
+# ClickHouse can append expressions and query context to any error. Use fixed messages for
+# new exposed codes; auditing the initial throw alone does not establish that raw text is safe.
CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {
0: ErrorCodeMeta("OK"),
1: ErrorCodeMeta("UNSUPPORTED_METHOD", category=QueryErrorCategory.USER_ERROR),
2: ErrorCodeMeta("UNSUPPORTED_PARAMETER"),
3: ErrorCodeMeta("UNEXPECTED_END_OF_FILE"),
4: ErrorCodeMeta("EXPECTED_END_OF_FILE"),
- # 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),
+ 6: ErrorCodeMeta(
+ "CANNOT_PARSE_TEXT",
+ user_safe="Cannot parse a value as the requested type. Check your input formats and type conversions.",
+ ),
7: ErrorCodeMeta("INCORRECT_NUMBER_OF_COLUMNS"),
8: ErrorCodeMeta("THERE_IS_NO_COLUMN"),
9: ErrorCodeMeta("SIZES_OF_COLUMNS_DOESNT_MATCH"),
@@ -395,10 +398,12 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {
35: ErrorCodeMeta("TOO_FEW_ARGUMENTS_FOR_FUNCTION", user_safe=True),
36: ErrorCodeMeta("BAD_ARGUMENTS", user_safe=True),
37: ErrorCodeMeta("UNKNOWN_ELEMENT_IN_AST"),
- 38: ErrorCodeMeta("CANNOT_PARSE_DATE", user_safe=True),
+ 38: ErrorCodeMeta("CANNOT_PARSE_DATE", user_safe="Cannot parse a date. Check your date values and their format."),
39: ErrorCodeMeta("TOO_LARGE_SIZE_COMPRESSED"),
40: ErrorCodeMeta("CHECKSUM_DOESNT_MATCH"),
- 41: ErrorCodeMeta("CANNOT_PARSE_DATETIME", user_safe=True),
+ 41: ErrorCodeMeta(
+ "CANNOT_PARSE_DATETIME", user_safe="Cannot parse a date and time. Check your datetime values and their format."
+ ),
42: ErrorCodeMeta("NUMBER_OF_ARGUMENTS_DOESNT_MATCH", user_safe=True),
43: ErrorCodeMeta("ILLEGAL_TYPE_OF_ARGUMENT", user_safe=True),
44: ErrorCodeMeta(
@@ -434,8 +439,9 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {
user_safe="Cannot convert one type to another in the query. Check the types in your comparisons and IN clauses.",
),
71: ErrorCodeMeta("CANNOT_WRITE_AFTER_END_OF_BUFFER"),
- # 72 stays internal: the CH message embeds the failing data value (see code 6 note).
- 72: ErrorCodeMeta("CANNOT_PARSE_NUMBER", category=QueryErrorCategory.USER_ERROR),
+ 72: ErrorCodeMeta(
+ "CANNOT_PARSE_NUMBER", user_safe="Cannot parse a number. Check for non-numeric values in your type conversions."
+ ),
73: ErrorCodeMeta("UNKNOWN_FORMAT"),
74: ErrorCodeMeta("CANNOT_READ_FROM_FILE_DESCRIPTOR"),
75: ErrorCodeMeta("CANNOT_WRITE_TO_FILE_DESCRIPTOR"),
@@ -478,15 +484,28 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {
117: ErrorCodeMeta("INCORRECT_DATA"),
119: ErrorCodeMeta("ENGINE_REQUIRED"),
120: ErrorCodeMeta("CANNOT_INSERT_VALUE_OF_DIFFERENT_SIZE_INTO_TUPLE"),
- 121: ErrorCodeMeta("UNSUPPORTED_JOIN_KEYS"),
+ 121: ErrorCodeMeta(
+ "UNSUPPORTED_JOIN_KEYS",
+ user_safe="Unsupported JOIN keys. Check the expressions and types used to join your tables.",
+ ),
122: ErrorCodeMeta("INCOMPATIBLE_COLUMNS", user_safe=True), # column types don't match expected schema
123: ErrorCodeMeta("UNKNOWN_TYPE_OF_AST_NODE"),
124: ErrorCodeMeta("INCORRECT_ELEMENT_OF_SET"),
- 125: ErrorCodeMeta("INCORRECT_RESULT_OF_SCALAR_SUBQUERY"),
+ 125: ErrorCodeMeta(
+ "INCORRECT_RESULT_OF_SCALAR_SUBQUERY",
+ user_safe="A scalar subquery returned more than one row, or an empty result that cannot be nullable. "
+ "Make sure the subquery returns a single value.",
+ ),
127: ErrorCodeMeta("ILLEGAL_INDEX"),
- 128: ErrorCodeMeta("TOO_LARGE_ARRAY_SIZE"),
+ 128: ErrorCodeMeta(
+ "TOO_LARGE_ARRAY_SIZE",
+ user_safe="An array has an invalid or unsupported size. Check array lengths and reduce the number of elements.",
+ ),
129: ErrorCodeMeta("FUNCTION_IS_SPECIAL"),
- 130: ErrorCodeMeta("CANNOT_READ_ARRAY_FROM_TEXT"),
+ 130: ErrorCodeMeta(
+ "CANNOT_READ_ARRAY_FROM_TEXT",
+ user_safe="Cannot parse an array. Check the format of the values converted to arrays.",
+ ),
131: ErrorCodeMeta("TOO_LARGE_STRING_SIZE"),
133: ErrorCodeMeta("AGGREGATE_FUNCTION_DOESNT_ALLOW_PARAMETERS"),
134: ErrorCodeMeta("PARAMETERS_TO_AGGREGATE_FUNCTIONS_MUST_BE_LITERALS"),
@@ -527,7 +546,10 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {
184: ErrorCodeMeta("ILLEGAL_AGGREGATION", user_safe=True),
186: ErrorCodeMeta("UNSUPPORTED_COLLATION_LOCALE"),
187: ErrorCodeMeta("COLLATION_COMPARISON_FAILED"),
- 190: ErrorCodeMeta("SIZES_OF_ARRAYS_DONT_MATCH"),
+ 190: ErrorCodeMeta(
+ "SIZES_OF_ARRAYS_DONT_MATCH",
+ user_safe="Array sizes do not match. Make sure arrays used together have the same number of elements.",
+ ),
191: ErrorCodeMeta("SET_SIZE_LIMIT_EXCEEDED"),
192: ErrorCodeMeta("UNKNOWN_USER"),
193: ErrorCodeMeta("WRONG_PASSWORD"),
@@ -676,7 +698,9 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {
373: ErrorCodeMeta("SESSION_IS_LOCKED"),
374: ErrorCodeMeta("INVALID_SESSION_TIMEOUT"),
375: ErrorCodeMeta("CANNOT_DLOPEN"),
- 376: ErrorCodeMeta("CANNOT_PARSE_UUID", user_safe=True),
+ 376: ErrorCodeMeta(
+ "CANNOT_PARSE_UUID", user_safe="Cannot parse a UUID. Check the format of the values converted to UUIDs."
+ ),
377: ErrorCodeMeta("ILLEGAL_SYNTAX_FOR_DATA_TYPE", user_safe=True),
378: ErrorCodeMeta("DATA_TYPE_CANNOT_HAVE_ARGUMENTS"),
380: ErrorCodeMeta("CANNOT_KILL"),
@@ -704,7 +728,10 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {
400: ErrorCodeMeta("CANNOT_STAT"),
401: ErrorCodeMeta("FEATURE_IS_NOT_ENABLED_AT_BUILD_TIME"),
402: ErrorCodeMeta("CANNOT_IOSETUP"),
- 403: ErrorCodeMeta("INVALID_JOIN_ON_EXPRESSION", category=QueryErrorCategory.USER_ERROR),
+ 403: ErrorCodeMeta(
+ "INVALID_JOIN_ON_EXPRESSION",
+ user_safe="Invalid JOIN ON expression. Check that the condition uses supported comparisons between the joined tables.",
+ ),
404: ErrorCodeMeta("BAD_ODBC_CONNECTION_STRING"),
406: ErrorCodeMeta("TOP_AND_LIMIT_TOGETHER"),
# Fixed message: the raw CH text can format a converted decimal value into the overflow error.
@@ -725,7 +752,10 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {
423: ErrorCodeMeta("CANNOT_GETTIMEOFDAY"),
424: ErrorCodeMeta("CANNOT_LINK"),
425: ErrorCodeMeta("SYSTEM_ERROR"),
- 427: ErrorCodeMeta("CANNOT_COMPILE_REGEXP"),
+ 427: ErrorCodeMeta(
+ "CANNOT_COMPILE_REGEXP",
+ user_safe="Cannot compile the regular expression. Check RE2 syntax, escaping, and the number of capturing groups.",
+ ),
429: ErrorCodeMeta("FAILED_TO_GETPWUID"),
430: ErrorCodeMeta("MISMATCHING_USERS_FOR_PROCESS_AND_DATA"),
431: ErrorCodeMeta("ILLEGAL_SYNTAX_FOR_CODEC_TYPE"),
@@ -737,7 +767,10 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {
437: ErrorCodeMeta("PROTOBUF_FIELD_NOT_REPEATED"),
438: ErrorCodeMeta("DATA_TYPE_CANNOT_BE_PROMOTED"),
439: ErrorCodeMeta("CANNOT_SCHEDULE_TASK", category=QueryErrorCategory.RATE_LIMITED),
- 440: ErrorCodeMeta("INVALID_LIMIT_EXPRESSION"),
+ 440: ErrorCodeMeta(
+ "INVALID_LIMIT_EXPRESSION",
+ user_safe="Invalid LIMIT or OFFSET expression. Use a constant number in the supported range.",
+ ),
441: ErrorCodeMeta("CANNOT_PARSE_DOMAIN_VALUE_FROM_STRING"),
442: ErrorCodeMeta("BAD_DATABASE_FOR_TEMPORARY_TABLE"),
443: ErrorCodeMeta("NO_COLUMNS_SERIALIZED_TO_PROTOBUF_FIELDS"),
@@ -763,7 +796,9 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: dict[int, ErrorCodeMeta] = {
464: ErrorCodeMeta("CANNOT_PARSE_ELF"),
465: ErrorCodeMeta("CANNOT_PARSE_DWARF"),
466: ErrorCodeMeta("INSECURE_PATH"),
- 467: ErrorCodeMeta("CANNOT_PARSE_BOOL", category=QueryErrorCategory.USER_ERROR),
+ 467: ErrorCodeMeta(
+ "CANNOT_PARSE_BOOL", user_safe="Cannot parse a boolean. Check for values other than true, false, 1, or 0."
+ ),
468: ErrorCodeMeta("CANNOT_PTHREAD_ATTR"),
469: ErrorCodeMeta("VIOLATED_CONSTRAINT"),
471: ErrorCodeMeta("INVALID_SETTING_VALUE"),
@@ -961,15 +996,20 @@ 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),
+ 675: ErrorCodeMeta(
+ "CANNOT_PARSE_IPV4", user_safe="Cannot parse an IPv4 address. Check the format of your IP addresses."
+ ),
+ 676: ErrorCodeMeta(
+ "CANNOT_PARSE_IPV6", user_safe="Cannot parse an IPv6 address. Check the format of your IP addresses."
+ ),
677: ErrorCodeMeta("THREAD_WAS_CANCELED"),
678: ErrorCodeMeta("IO_URING_INIT_FAILED"),
679: ErrorCodeMeta("IO_URING_SUBMIT_ERROR"),
690: ErrorCodeMeta("MIXED_ACCESS_PARAMETER_TYPES"),
- # Stays internal: the CH message embeds the offending enum value (see code 6 note).
- 691: ErrorCodeMeta("UNKNOWN_ELEMENT_OF_ENUM", category=QueryErrorCategory.USER_ERROR),
+ 691: ErrorCodeMeta(
+ "UNKNOWN_ELEMENT_OF_ENUM",
+ user_safe="A value is not a member of the enum. Check the allowed values of the enum type.",
+ ),
692: ErrorCodeMeta("TOO_MANY_MUTATIONS"),
693: ErrorCodeMeta("AWS_ERROR"),
694: ErrorCodeMeta("ASYNC_LOAD_CYCLE"),
diff --git a/posthog/test/test_errors.py b/posthog/test/test_errors.py
index 4207e5974268..79f85e4a221b 100644
--- a/posthog/test/test_errors.py
+++ b/posthog/test/test_errors.py
@@ -2,6 +2,9 @@ from clickhouse_driver.errors import ServerException
from parameterized import parameterized
from posthog.errors import (
+ CHQueryErrorCannotParseBool,
+ CHQueryErrorCannotParseUuid,
+ CHQueryErrorInvalidJoinOnExpression,
ExposedCHQueryError,
InternalCHQueryError,
QueryErrorCategory,
@@ -13,9 +16,6 @@ from posthog.errors import (
class TestWrapClickhouseQueryError:
@parameterized.expand(
[
- # 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"),
@@ -40,31 +40,115 @@ class TestWrapClickhouseQueryError:
@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.
+ (
+ 6,
+ "CANNOT_PARSE_TEXT",
+ "Cannot parse a value as the requested type. Check your input formats and type conversions.",
+ ),
+ (38, "CANNOT_PARSE_DATE", "Cannot parse a date. Check your date values and their format."),
+ (41, "CANNOT_PARSE_DATETIME", "Cannot parse a date and time. Check your datetime values and their format."),
(69, "ARGUMENT_OUT_OF_BOUND", "An argument is out of bounds."),
(
70,
"CANNOT_CONVERT_TYPE",
"Cannot convert one type to another in the query. Check the types in your comparisons and IN clauses.",
),
+ (
+ 72,
+ "CANNOT_PARSE_NUMBER",
+ "Cannot parse a number. Check for non-numeric values in your type conversions.",
+ ),
+ (
+ 121,
+ "UNSUPPORTED_JOIN_KEYS",
+ "Unsupported JOIN keys. Check the expressions and types used to join your tables.",
+ ),
+ (
+ 125,
+ "INCORRECT_RESULT_OF_SCALAR_SUBQUERY",
+ "A scalar subquery returned more than one row, or an empty result that cannot be nullable. "
+ "Make sure the subquery returns a single value.",
+ ),
+ (
+ 128,
+ "TOO_LARGE_ARRAY_SIZE",
+ "An array has an invalid or unsupported size. Check array lengths and reduce the number of elements.",
+ ),
+ (
+ 130,
+ "CANNOT_READ_ARRAY_FROM_TEXT",
+ "Cannot parse an array. Check the format of the values converted to arrays.",
+ ),
+ (
+ 190,
+ "SIZES_OF_ARRAYS_DONT_MATCH",
+ "Array sizes do not match. Make sure arrays used together have the same number of elements.",
+ ),
+ (376, "CANNOT_PARSE_UUID", "Cannot parse a UUID. Check the format of the values converted to UUIDs."),
+ (
+ 403,
+ "INVALID_JOIN_ON_EXPRESSION",
+ "Invalid JOIN ON expression. Check that the condition uses supported comparisons between the joined tables.",
+ ),
(407, "DECIMAL_OVERFLOW", "Decimal overflow while executing query."),
+ (
+ 427,
+ "CANNOT_COMPILE_REGEXP",
+ "Cannot compile the regular expression. Check RE2 syntax, escaping, and the number of capturing groups.",
+ ),
+ (
+ 440,
+ "INVALID_LIMIT_EXPRESSION",
+ "Invalid LIMIT or OFFSET expression. Use a constant number in the supported range.",
+ ),
+ (467, "CANNOT_PARSE_BOOL", "Cannot parse a boolean. Check for values other than true, false, 1, or 0."),
(
491,
"UNACCEPTABLE_URL",
"PostHog can't read from this storage host. Check the files URL pattern on the table points at "
"S3, Google Cloud Storage, Cloudflare R2, or Azure Blob Storage.",
),
+ (675, "CANNOT_PARSE_IPV4", "Cannot parse an IPv4 address. Check the format of your IP addresses."),
+ (676, "CANNOT_PARSE_IPV6", "Cannot parse an IPv6 address. Check the format of your IP addresses."),
+ (
+ 691,
+ "UNKNOWN_ELEMENT_OF_ENUM",
+ "A value is not a member of the enum. Check the allowed values of the enum type.",
+ ),
]
)
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)
+ raw_message = (
+ f"DB::Exception: {name}: cannot convert 'private-row-value'. "
+ "While processing SELECT * FROM s3('https://example.com/private.parquet?token=fake-signed-token', "
+ "'fake-access-key', 'fake-secret-key') SETTINGS custom_setting='fake-setting-secret'. "
+ "Received from private-host.example.com:9000. Stack trace: private-stack-frame"
+ )
+ err = ServerException(raw_message, code=code, nested=ServerException("fake-nested-secret", code=1000))
wrapped = wrap_clickhouse_query_error(err)
assert isinstance(wrapped, ExposedCHQueryError)
assert str(wrapped) == message
+ assert wrapped.message == message
+ assert wrapped.code == code
+ assert wrapped.code_name == name.lower()
+ assert wrapped.nested is None
+ assert look_up_clickhouse_error_code_meta(err).get_category() == QueryErrorCategory.USER_ERROR
+
+ @parameterized.expand(
+ [
+ (376, CHQueryErrorCannotParseUuid),
+ (403, CHQueryErrorInvalidJoinOnExpression),
+ (467, CHQueryErrorCannotParseBool),
+ ]
+ )
+ def test_fixed_messages_preserve_importable_exception_types(
+ self, code: int, error_type: type[ExposedCHQueryError]
+ ) -> None:
+ wrapped = wrap_clickhouse_query_error(ServerException("private-row-value", code=code))
+
+ assert isinstance(wrapped, error_type)
@parameterized.expand(
[
@@ -73,13 +157,13 @@ class TestWrapClickhouseQueryError:
# 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"),
- (72, "CANNOT_PARSE_NUMBER"),
- (675, "CANNOT_PARSE_IPV4"),
- (676, "CANNOT_PARSE_IPV6"),
- (691, "UNKNOWN_ELEMENT_OF_ENUM"),
+ (115, "UNKNOWN_SETTING"),
+ (497, "ACCESS_DENIED"),
+ (499, "S3_ERROR"),
+ (516, "AUTHENTICATION_FAILED"),
+ (1000, "POCO_EXCEPTION"),
+ (1001, "STD_EXCEPTION"),
+ (9999, "FUTURE_ERROR"),
]
)
def test_codes_stay_internal(self, code: int, name: str) -> None:
diff --git a/products/feature_flags/backend/test/test_user_blast_radius.py b/products/feature_flags/backend/test/test_user_blast_radius.py
index 0ebcb7d22e42..f9a3168771a6 100644
--- a/products/feature_flags/backend/test/test_user_blast_radius.py
+++ b/products/feature_flags/backend/test/test_user_blast_radius.py
@@ -20,21 +20,30 @@ class TestUnevaluableFiltersAsValidationErrors(SimpleTestCase):
@parameterized.expand(
[
- ("cannot_parse_text", 6),
- ("cannot_parse_number", 72),
+ (
+ "cannot_parse_text",
+ 6,
+ "Cannot parse a value as the requested type. Check your input formats and type conversions.",
+ ),
+ (
+ "cannot_parse_number",
+ 72,
+ "Cannot parse a number. Check for non-numeric values in your type conversions.",
+ ),
]
)
- def test_clickhouse_value_parse_failure_surfaces_as_a_caller_error(self, _name, code):
- # A numeric operator against a null/non-numeric filter value fails the Float64 cast at
- # execution; these codes wrap to InternalCHQueryError (not Exposed), so they used to 500.
- # The 400 body must carry only the useful message: the DB::Exception framing and any
- # server stack trace tail are stripped, matching what ExposedCHQueryError exposes.
+ def test_clickhouse_value_parse_failure_surfaces_as_a_caller_error(
+ self, _name: str, code: int, expected_message: str
+ ) -> None:
raw = "DB::Exception: Cannot parse NaN: converting 'None' to Float64. Stack trace:\n0. DB::Exception::Exception"
err = wrap_clickhouse_query_error(ServerException(raw, code=code))
with self.assertRaises(ValidationError) as ctx, unevaluable_filters_as_validation_errors():
raise err
message = str(ctx.exception)
- self.assertIn("Cannot parse NaN", message)
+ assert isinstance(ctx.exception.detail, dict)
+ self.assertEqual(ctx.exception.detail["filters"], expected_message)
+ self.assertNotIn("NaN", message)
+ self.assertNotIn("None", message)
self.assertNotIn("Stack trace", message)
self.assertNotIn("DB::Exception", message)
diff --git a/products/feature_flags/backend/user_blast_radius.py b/products/feature_flags/backend/user_blast_radius.py
index de498803b37f..d22167063647 100644
--- a/products/feature_flags/backend/user_blast_radius.py
+++ b/products/feature_flags/backend/user_blast_radius.py
@@ -97,8 +97,8 @@ def sampled_person_blast_radius(team: Team, filter: Filter, query_type: str) ->
# ClickHouse codes for "this literal can't be parsed as the column's type": 6 CANNOT_PARSE_TEXT,
# 72 CANNOT_PARSE_NUMBER — e.g. a numeric operator (gt/lt) against a null or non-numeric filter
-# value casts 'None' to Float64 and fails deterministically. Both are classified USER_ERROR in
-# posthog/errors.py but not user_safe, so they wrap to InternalCHQueryError, not Exposed.
+# value casts 'None' to Float64 and fails deterministically. These codes also identify failures
+# in stored flag dependencies, which must fall back to sizing without the dependency.
_VALUE_PARSE_CH_ERROR_CODES = frozenset({6, 72})