From ff37e8f40c88a1670b08782df8614a6274b859ff Mon Sep 17 00:00:00 2001 From: Klaas van Schelven Date: Thu, 19 Sep 2024 12:39:06 +0200 Subject: [PATCH] Remove 'understandable_json_errors utils turns out it already exists (best_match) --- bugsink/utils.py | 18 ------------------ ingest/management/commands/send_json.py | 5 +++-- ingest/views.py | 7 +++++-- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/bugsink/utils.py b/bugsink/utils.py index dbf771f..baca042 100644 --- a/bugsink/utils.py +++ b/bugsink/utils.py @@ -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 != ""]) diff --git a/ingest/management/commands/send_json.py b/ingest/management/commands/send_json.py index 9c60ae2..942c421 100644 --- a/ingest/management/commands/send_json.py +++ b/ingest/management/commands/send_json.py @@ -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 diff --git a/ingest/views.py b/ingest/views.py index cc6b1bb..458c39b 100644 --- a/ingest/views.py +++ b/ingest/views.py @@ -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