Only catch TypeError/ValueError for object lookups#6028
Only catch TypeError/ValueError for object lookups#6028carltongibson merged 3 commits intoencode:masterfrom
Conversation
|
Needs a test though (https://codecov.io/gh/encode/django-rest-framework/pull/6028/diff). |
lovelydinosaur
left a comment
There was a problem hiding this comment.
Neat route around, yup.
rest_framework/exceptions.py
Outdated
|
|
||
| class ObjectValueError(Exception): | ||
| """ | ||
| A value was not suitable for requesting an object. |
There was a problem hiding this comment.
I think this could have a clearer docstring. Mention that we use it to indicate when invalid values are passed to ‘queryset.get()’.
There was a problem hiding this comment.
Are the new docstrings more clear? Generally, these exceptions will only be caught internally, so it should be fine?
b8a200e to
20a0ed3
Compare
|
Thanks @blueyed - test added. |
|
@rpkilby See #6050 for making this more accessible via Github's status API - i.e. it would show up with "Show all checks". |
|
The updated tests should sufficiently cover the changeset. |
|
What about the following to please coverage? (used elsewhere already) diff --git i/tests/test_relations.py w/tests/test_relations.py
index 3c76f009..9326b565 100644
--- i/tests/test_relations.py
+++ w/tests/test_relations.py
@@ -200,7 +200,7 @@ def test_hyperlinked_related_lookup_does_not_exist(self):
def test_hyperlinked_related_internal_type_error(self):
class Field(serializers.HyperlinkedRelatedField):
def get_object(self, incorrect, signature):
- pass
+ raise NotImplementedError() # pragma: no cover
field = Field(view_name='example', queryset=self.queryset)
with pytest.raises(TypeError): |
|
Makes sense - raising |
1e5b680 to
1e789d0
Compare
|
btw, merged the setup.cfg changes in another PR. |
1e789d0 to
43f9df0
Compare
* Only catch TypeError/ValueError for object lookups * Test wrapped TypeError/ValueError handling * Raise NotImplementedError in tests instead of pass
Fixes #6025. Basically,
get_object()should handle the type/value error from.get()by reraising as a more specific exception type.cc @tomchristie, @tveastman