Skip to content

Commit

Permalink
switches to a more human-readable error message for #4816 (#6113)
Browse files Browse the repository at this point in the history
* switches to a more human-readable error message for #4816

we are intermittently receiving undefined as the request body from the session recorder. This makes the error for that more readable
  • Loading branch information
pauldambra authored Sep 27, 2021
1 parent 9c8832f commit b913095
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
32 changes: 32 additions & 0 deletions posthog/test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from django.test import TestCase
from django.test.client import RequestFactory
from freezegun import freeze_time

from posthog.exceptions import RequestParsingError
from posthog.models import EventDefinition
from posthog.test.base import BaseTest
from posthog.utils import (
get_available_timezones_with_offsets,
get_default_event_name,
load_data_from_request,
mask_email_address,
relative_date_parse,
)
Expand Down Expand Up @@ -78,3 +81,32 @@ def test_prefer_pageview(self):
EventDefinition.objects.create(name="$pageview", team=self.team)
EventDefinition.objects.create(name="$screen", team=self.team)
self.assertEqual(get_default_event_name(), "$pageview")


class TestLoadDataFromRequest(TestCase):
def test_fails_to_JSON_parse_the_literal_string_undefined_when_not_compressed(self):
"""
load_data_from_request assumes that any data
that has been received (and possibly decompressed) from the body
can be parsed as JSON
this test maintains the default (and possibly undesirable) behaviour for the uncompressed case
"""
rf = RequestFactory()
post_request = rf.post("/s/", "undefined", "text/plain")

with self.assertRaises(RequestParsingError) as ctx:
load_data_from_request(post_request)

self.assertEqual("Invalid JSON: Expecting value: line 1 column 1 (char 0)", str(ctx.exception))

def test_raises_specific_error_for_the_literal_string_undefined_when_compressed(self):
rf = RequestFactory()
post_request = rf.post("/s/?compression=gzip-js", "undefined", "text/plain")

with self.assertRaises(RequestParsingError) as ctx:
load_data_from_request(post_request)

self.assertEqual(
"data being loaded from the request body for decompression is the literal string 'undefined'",
str(ctx.exception),
)
5 changes: 5 additions & 0 deletions posthog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ def load_data_from_request(request):
compression = compression.lower()

if compression == "gzip" or compression == "gzip-js":
if data == b"undefined":
raise RequestParsingError(
"data being loaded from the request body for decompression is the literal string 'undefined'"
)

try:
data = gzip.decompress(data)
except (EOFError, OSError) as error:
Expand Down

0 comments on commit b913095

Please sign in to comment.