get_attribute swallows tracebacks for KeyError/AttributeError #7745
Replies: 11 comments
-
Hmm, I think I have a possible solution. First of all this error message logic could be moved to the base Secondly, instead of catching Then we can be precise about the issue we're trying to catch, and avoid confusing a exception thrown by code triggered by the getattr or getitem / property call, for an error retrieving the key/attr itself. How does that sound? |
Beta Was this translation helpful? Give feedback.
-
I'm having this issue as well, just here to add some more background. This is somewhat related to #2598, but instead of callables raising an attribute exception this is on property fields. Here's a reproduction: """
requirements.txt:
Django==1.8.2
djangorestframework==3.1.3
"""
from django.conf import settings
SETTINGS = dict(
DATABASES = {},
DEBUG=True,
TEMPLATE_DEBUG=True,
ROOT_URLCONF = None,
)
settings.configure(**SETTINGS)
from rest_framework import serializers
class ExampleSerializer(serializers.Serializer):
example_property = serializers.CharField(read_only=True)
example_fail_property = serializers.CharField(read_only=True)
class ExampleInstance(object):
@property
def example_property(self):
return "foobar"
@property
def example_fail_property(self):
raise AttributeError('property failed')
serializer = ExampleSerializer(ExampleInstance())
print(serializer.data.items()) # ItemsView({'example_property': 'foobar'}) |
Beta Was this translation helpful? Give feedback.
-
@johtso my first thought is that is feels wrong to have a |
Beta Was this translation helpful? Give feedback.
-
Just another note as I'm digging further into this: this problem only manifests for |
Beta Was this translation helpful? Give feedback.
-
Just got bitten by this again.. any more thoughts on this problem/solution? |
Beta Was this translation helpful? Give feedback.
-
just got bit. ouch. |
Beta Was this translation helpful? Give feedback.
-
Not obvious how to proceed with this. Thoughts/suggestions welcome. |
Beta Was this translation helpful? Give feedback.
-
Just for any googlers arriving here trying to solve a KeyError on get_attribute, it may be due to accessing serializer.data in a situation where validation has already been done. In that case you should be using serializer.validated_data. http://stackoverflow.com/questions/32236958/keyerror-on-relation-field-in-django-rest |
Beta Was this translation helpful? Give feedback.
-
Something to think about later - #5600 could be adapted to fix this. |
Beta Was this translation helpful? Give feedback.
-
@chidg: Good point; still appears in 2018. |
Beta Was this translation helpful? Give feedback.
-
Was caught by this. Some other package I rely on for a property had a non-obvious breaking change. My serializer tests failed instantly -- just dropped the field from A warning log at the least would have been immensely helpful. |
Beta Was this translation helpful? Give feedback.
-
get_attribute
currently catchesKeyError
s andAttributeError
s when trying to retrieve fields, assuming that those errors resulted from the top level attribute getting or key getting.The problem with this is that if anything else inside a property causes one of those errors, the traceback is swallowed and it's a nightmare to debug!
Is there maybe a way to make sure that the exception doesn't come from deeper down?
Contrived illustration:
Beta Was this translation helpful? Give feedback.
All reactions