diff --git a/.sampo/changesets/exception-long-context-lines.md b/.sampo/changesets/exception-long-context-lines.md new file mode 100644 index 000000000..ea740a8a0 --- /dev/null +++ b/.sampo/changesets/exception-long-context-lines.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Exception autocapture now sends a truncated string for source lines over 1024 bytes in a frame's `context_line`, `pre_context` and `post_context`. Before, those lines were sent as `null`. diff --git a/posthog/exception_utils.py b/posthog/exception_utils.py index d462d23cc..107973953 100644 --- a/posthog/exception_utils.py +++ b/posthog/exception_utils.py @@ -413,7 +413,7 @@ def get_lines_from_file( loader=None, # type: Optional[Any] module=None, # type: Optional[str] ): - # type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]] + # type: (...) -> Tuple[List[str], Optional[str], List[str]] context_lines = 5 source = _get_source_lines(filename, loader, module) @@ -423,14 +423,20 @@ def get_lines_from_file( lower_bound = max(0, lineno - context_lines) upper_bound = min(lineno + 1 + context_lines, len(source)) + def strip_line(line): + # type: (str) -> str + # strip_string wraps a truncated line in an AnnotatedValue, which the + # event payload can't serialize, so keep only the truncated text. + stripped = strip_string(line.strip("\r\n"), max_length=max_length) + if isinstance(stripped, AnnotatedValue): + return cast(str, stripped.value) + return stripped + try: - pre_context = [ - strip_string(line.strip("\r\n"), max_length=max_length) - for line in source[lower_bound:lineno] - ] - context_line = strip_string(source[lineno].strip("\r\n"), max_length=max_length) + pre_context = [strip_line(line) for line in source[lower_bound:lineno]] + context_line = strip_line(source[lineno]) post_context = [ - strip_string(line.strip("\r\n"), max_length=max_length) + strip_line(line) for line in source[(lineno + 1) : upper_bound] # noqa: E203 ] return pre_context, context_line, post_context @@ -464,7 +470,7 @@ def get_source_context( tb_lineno, # type: int max_value_length=None, # type: Optional[int] ): - # type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]] + # type: (...) -> Tuple[List[str], Optional[str], List[str]] try: abs_path = frame.f_code.co_filename # type: Optional[str] except Exception: diff --git a/posthog/test/test_exception_utils.py b/posthog/test/test_exception_utils.py index e719c8c31..063194618 100644 --- a/posthog/test/test_exception_utils.py +++ b/posthog/test/test_exception_utils.py @@ -61,6 +61,23 @@ def unavailable(module): ) == ([], "current", ["after"]) +def test_source_context_truncates_long_lines_to_strings(tmp_path): + long_line = "x" * 1100 + source = tmp_path / "application.py" + source.write_text("\n".join([long_line, long_line, long_line])) + + pre_context, context_line, post_context = exception_utils.get_lines_from_file( + str(source), 1 + ) + + truncated = "x" * 1021 + "..." + assert (pre_context, context_line, post_context) == ( + [truncated], + truncated, + [truncated], + ) + + def test_source_context_handles_changed_files(tmp_path): source = tmp_path / "application.py" source.write_text("current\n")