Skip to content

Commit

Permalink
Simplify json error reporting (#3286)
Browse files Browse the repository at this point in the history
Json validation errors used to include the instance (the submitted
json), the schema and the error. This changes the reporting to only
print the error message without the schema and instance. This is a
temporary fix until this is implemented:
python-jsonschema/jsonschema#1218

It also standardizes the message for `FILE_COPY_STATUS` notifications,
opting for the less verbose version.
  • Loading branch information
amickan committed Mar 25, 2024
1 parent 9c7930c commit 9b4f113
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
8 changes: 4 additions & 4 deletions app/grandchallenge/components/tasks.py
Expand Up @@ -1049,7 +1049,7 @@ def add_file_to_component_interface_value(
civ.full_clean()
except ValidationError as e:
civ.delete()
error = str(e)
error = str(e.message)
else:
user_upload.copy_object(to_field=civ.file)
civ.save()
Expand Down Expand Up @@ -1200,7 +1200,7 @@ def add_file_to_object(
civ = ComponentInterfaceValue.objects.get(pk=civ_pk)
object.values.remove(civ)
except ValidationError as e:
error = str(e)
error = str(e.message)

if error is not None:
Notification.send(
Expand All @@ -1209,7 +1209,7 @@ def add_file_to_object(
message=f"File for interface {interface.title} failed validation.",
target=object.base_object,
description=(
f"File for interface {interface.title} added to {object_pk} "
f"in {object.base_object.title} failed validation:\n{error}."
f"File for interface {interface.title} "
f"failed validation:\n{error}."
),
)
4 changes: 3 additions & 1 deletion app/grandchallenge/core/validators.py
Expand Up @@ -161,7 +161,9 @@ def __call__(self, value):
try:
validate(value, self.schema, registry=self.registry)
except JSONValidationError as e:
raise ValidationError(f"JSON does not fulfill schema: {e}")
raise ValidationError(
f"JSON does not fulfill schema: instance {e.message.replace(str(e.instance) + ' ', '')}"
)

def __eq__(self, other):
return isinstance(other, JSONValidator) and self.schema == other.schema
Expand Down
2 changes: 1 addition & 1 deletion app/tests/components_tests/test_serializers.py
Expand Up @@ -346,7 +346,7 @@ def test_civ_post_value_required(kind):

# verify
assert not serializer.is_valid()
assert "JSON does not fulfill schema: None is not of type" in str(
assert "JSON does not fulfill schema: instance is not of type" in str(
serializer.errors["__all__"][0]
)

Expand Down

0 comments on commit 9b4f113

Please sign in to comment.