send_json: print understandable json errors

at least: this works in the current set of circumstances
This commit is contained in:
Klaas van Schelven
2024-09-16 14:20:21 +02:00
parent 519c09b662
commit 5f29a20765

View File

@@ -16,6 +16,24 @@ from bugsink.streams import compress_with_zlib, WBITS_PARAM_FOR_GZIP, WBITS_PARA
from projects.models import Project
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 != ""])
class Command(BaseCommand):
help = "Send raw events to a sentry-compatible server; events can be sources from the filesystem or your DB."
@@ -44,7 +62,7 @@ class Command(BaseCommand):
jsonschema.validate(data, schema)
except jsonschema.ValidationError as e:
self.stderr.write("%s %s" % (repr(e), identifier))
self.stderr.write("%s %s" % (understandable_json_error(e), identifier))
return False
return True