Skip to content

Don't catch exceptions in get_queryset #7480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2020
Merged

Don't catch exceptions in get_queryset #7480

merged 1 commit into from
Oct 10, 2020

Conversation

thomasleese
Copy link
Contributor

In the to_internal_value method of the primary key and slug related fields, TypeErrors and ValueErrors are caught from self.get_queryset().get(...) and presented to the user. This works fine for most cases, but can cause problems if the exception is coming from self.get_queryset() rather than from the .get(...).

It means errors in the get_queryset method can be hidden and presented back to the user as though, for example, the input provided to the to_internal_value was the wrong type, whereas in reality there's a bug in the get_queryset method and therefore it should bubble up and be exposed as a server error.

I've decided to fix this because twice now I've had to debug why I'm seeing invalid_type errors from my serializer (errors like wrong pk type - int when the pk type on my model is int) when the real problem was a bug in my custom get_queryset method.

In the `to_internal_value` method of the primary key and slug related fields, `TypeError`s and `ValueError`s are caught from `self.get_queryset().get(...)` and presented to the user. This works fine for most cases, but can cause problems if the exception is coming from `self.get_queryset()` rather than from the `.get(...)`.

It means errors in the `get_queryset` method can be hidden and presented back to the user as though, for example, the input provided to the `to_internal_value` was the wrong type, whereas in reality there's a bug in the `get_queryset` method and therefore it should bubble up and be exposed as a server error.

I've decided to fix this because twice now I've had to debug why I'm seeing `invalid_type` errors from my serializer (errors like `wrong pk type - int` when the `pk` type on my model is `int`) when the real problem was a bug in my custom `get_queryset` method.
@adamchainz
Copy link
Contributor

This looks good! Could you add a tests for each of the classes where the get_queryset() method raises an exception, and check it is indeed raise with pytest.raises?

Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

Personally I'm okay with this without adding extra test cases, since it self evidently is an improvement to ensure that the try...except is as narrowly scoped as possible.

@adamchainz
Copy link
Contributor

Personally I'm okay with this without adding extra test cases, since it self evidently is an improvement to ensure that the try...except is as narrowly scoped as possible.

Fair enough. I guess a regression is very unlikely.

@tomchristie tomchristie merged commit eff97ef into encode:master Oct 10, 2020
@thomasleese thomasleese deleted the patch-1 branch October 10, 2020 17:13
@tomchristie tomchristie mentioned this pull request Oct 13, 2020
sigvef pushed a commit to sigvef/django-rest-framework that referenced this pull request Dec 3, 2022
In the `to_internal_value` method of the primary key and slug related fields, `TypeError`s and `ValueError`s are caught from `self.get_queryset().get(...)` and presented to the user. This works fine for most cases, but can cause problems if the exception is coming from `self.get_queryset()` rather than from the `.get(...)`.

It means errors in the `get_queryset` method can be hidden and presented back to the user as though, for example, the input provided to the `to_internal_value` was the wrong type, whereas in reality there's a bug in the `get_queryset` method and therefore it should bubble up and be exposed as a server error.

I've decided to fix this because twice now I've had to debug why I'm seeing `invalid_type` errors from my serializer (errors like `wrong pk type - int` when the `pk` type on my model is `int`) when the real problem was a bug in my custom `get_queryset` method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants