Hey,
When we import fixtures and
the input data is wrong (ValueError), we get something along the
following lines. Say the field is supposed to be an int and contains
instead an empty string as opposed to null/None:
Error
Traceback (most recent call last):
File "/run/media..../venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 1787, in to_python
return int(value)
ValueError: invalid literal for int() with base 10: ''
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/run/media/fv/hdd-2/GIT/VOLTEC-WEBAPP/app/venv/lib/python3.10/site-packages/django/core/serializers/python.py", line 142, in Deserializer
data[field.name] = field.to_python(field_value)
File "/run/media/fv/hdd-2/GIT/VOLTEC-WEBAPP/app/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 1789, in to_python
raise exceptions.ValidationError(
django.core.exceptions.ValidationError: ['“” value must be an integer.']
During handling of the above exception, another exception occurred: (... more similar output...)
This is raised for instance in venv/lib/python3.10/site-packages/django/core/serializers/python.py line 142 (and similarly in lines above of the same method):
try:
data[field.name] = field.to_python(field_value)
except Exception as e:
raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), field_value)
So we ultimately raise this exception:
class DeserializationError(Exception):
"""Something bad happened during deserialization."""
@classmethod
def WithData(cls, original_exc, model, fk, field_value):
"""
Factory method for creating a deserialization error which has a more
explanatory message.
"""
return cls("%s: (%s:pk=%s) field_value was '%s'" % (original_exc, model, fk, field_value))
At
least up to the point where the exception is raised (in
/serializers/python.py), we know which field exactly caused the error.
However, in the final error outputs, that information isn't logged
anywhere. How hard/easy would it be to either:
1 - Pass that info along to the WithData exception so that it can log it properly and yield that useful debugging info
2
- create a new custom exception, which would log the fieldname info, in
case modifying WithData isn't easy/possible (for instance raised in
other context where fieldname wouldn't be available)?
Thanks,