0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-30 19:41:46 +01:00

chore(api): Pass on "memory exceeded" errors to the user (#19907)

* chore(api): Pass on "memory exceeded" errors to the user

* Add test and fix issue
This commit is contained in:
Michael Matloka 2024-01-22 17:50:31 +01:00 committed by GitHub
parent ed87468351
commit 5ea8a3e1a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 5 deletions

View File

@ -26,6 +26,15 @@ from posthog.errors import wrap_query_error
"Code: 9999.\nSyntax error",
9999,
),
(
ServerException(
"Memory limit (for query) exceeded: would use 42.00 GiB (attempt to allocate chunk of 16757643 bytes), maximum: 42.00 GiB.",
code=241,
),
"CHQueryErrorMemoryLimitExceeded",
"Query exceeds memory limits. Tip: Specifying a narrower time range helps most of the time.",
241,
),
],
)
def test_wrap_query_error(error, expected_type, expected_message, expected_code):

View File

@ -18,15 +18,24 @@ class InternalCHQueryError(ServerException):
class ExposedCHQueryError(InternalCHQueryError):
def __str__(self) -> str:
message: str = self.message
start_index = message.index("DB::Exception:") + len("DB::Exception:")
end_index = message.index("Stack trace:")
try:
start_index = message.index("DB::Exception:") + len("DB::Exception:")
except ValueError:
start_index = 0
try:
end_index = message.index("Stack trace:")
except ValueError:
end_index = len(message)
return self.message[start_index:end_index].strip()
@dataclass
class ErrorCodeMeta:
name: str
user_safe: bool = False # Whether this error code is safe to show to the user and couldn't be caught at HogQL level
user_safe: bool | str = False
"""Whether this error code is safe to show to the user and couldn't be caught at HogQL level.
If a string is set, it will be used as the error message instead of the ClickHouse one.
"""
def wrap_query_error(err: Exception) -> Exception:
@ -44,7 +53,8 @@ def wrap_query_error(err: Exception) -> Exception:
meta = look_up_error_code_meta(err)
name = f"CHQueryError{meta.name.replace('_', ' ').title().replace(' ', '')}"
processed_error_class = ExposedCHQueryError if meta.user_safe else InternalCHQueryError
return type(name, (processed_error_class,), {})(err.message, code=err.code, code_name=meta.name.lower())
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())
return err
@ -309,7 +319,10 @@ CLICKHOUSE_ERROR_CODE_LOOKUP: Dict[int, ErrorCodeMeta] = {
238: ErrorCodeMeta("FORMAT_VERSION_TOO_OLD"),
239: ErrorCodeMeta("CANNOT_MUNMAP"),
240: ErrorCodeMeta("CANNOT_MREMAP"),
241: ErrorCodeMeta("MEMORY_LIMIT_EXCEEDED"),
241: ErrorCodeMeta(
"MEMORY_LIMIT_EXCEEDED",
user_safe="Query exceeds memory limits. Tip: Specifying a narrower time range helps most of the time.",
),
242: ErrorCodeMeta("TABLE_IS_READ_ONLY"),
243: ErrorCodeMeta("NOT_ENOUGH_SPACE"),
244: ErrorCodeMeta("UNEXPECTED_ZOOKEEPER_ERROR"),