Remove 'understandable_json_errors utils

turns out it already exists (best_match)
This commit is contained in:
Klaas van Schelven
2024-09-19 12:39:06 +02:00
parent febbbc67c7
commit ff37e8f40c
3 changed files with 8 additions and 22 deletions

View File

@@ -31,21 +31,3 @@ def deduce_allowed_hosts(base_url):
# localhost:8000 is.
return ["*"]
return [url.hostname]
def understandable_json_error(e):
# When a JSON schema contains many anyOfs, the default error message does not contain any useful information
# (despite containing a lot of information). This function recursively traverses the "context" to extract, what I
# found in Sept 2024, to be the most useful information.
if e.context == []:
if e.message.endswith("is not of type 'null'"):
# when you implement 'nullable' as an anyOf with null, this will be half of the error messages, but not the
# useful half. So we just ignore it.
return ""
# no more children, we're at the node, let's return the actually-interesting information
return ("%s: " % e.json_path) + e.message
# we have children, let's recurse
return "\n".join([s for s in [understandable_json_error(suberror) for suberror in e.context] if s != ""])

View File

@@ -12,7 +12,6 @@ from django.conf import settings
from compat.dsn import get_store_url, get_envelope_url, get_header_value
from bugsink.streams import compress_with_zlib, WBITS_PARAM_FOR_GZIP, WBITS_PARAM_FOR_DEFLATE
from bugsink.utils import understandable_json_error
from projects.models import Project
@@ -45,7 +44,9 @@ class Command(BaseCommand):
jsonschema.validate(data, schema)
except jsonschema.ValidationError as e:
self.stderr.write("%s %s" % (understandable_json_error(e), identifier))
best = jsonschema.exceptions.best_match([e])
self.stderr.write("%s: %s %s" % (best.json_path, best.message, identifier))
return False
return True

View File

@@ -28,7 +28,6 @@ from bugsink.transaction import immediate_atomic, delay_on_commit
from bugsink.exceptions import ViolatedExpectation
from bugsink.streams import content_encoding_reader, MaxDataReader, MaxDataWriter, NullWriter, MaxLengthExceeded
from bugsink.app_settings import get_settings
from bugsink.utils import understandable_json_error
from events.models import Event
from events.retention import evict_for_max_events, should_evict
@@ -148,7 +147,11 @@ class BaseIngestAPIView(View):
try:
jsonschema.validate(data_to_validate, get_schema())
except jsonschema.ValidationError as inner_e:
raise ValidationError(understandable_json_error(inner_e), code="invalid_event_data") from inner_e
best = jsonschema.exceptions.best_match([inner_e])
# we raise 'from best' here; this does lose some information w.r.t. 'from inner_e', but it's my
# belief that it's not useful info we're losing. similarly, but more so for 'fastjsonschema_e'.
raise ValidationError(best.json_path + ": " + best.message, code="invalid_event_data") from best
# in the (presumably not-happening) case that our fallback validation succeeds, fail w/o useful message
raise ValidationError(fastjsonschema_e.message, code="invalid_event_data") from fastjsonschema_e