Skip to content
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

Return status 404 instead of 500 when media not found #3552

Merged
merged 1 commit into from
Dec 19, 2023
Merged

Conversation

dhruvkb
Copy link
Member

@dhruvkb dhruvkb commented Dec 19, 2023

Fixes

Fixes #3520 by @AetherUnbound
Fixes #3349 by @obulat

Description

This PR changes the 500 exception raised by the /related endpoint to 404 instead because that's more appropriate for when the identifier does not point to a media item.

Testing Instructions

Visit the non-existent related endpoint and observe the response code. See the original issue for examples.

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • I ran the DAG documentation generator (if applicable).

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@dhruvkb dhruvkb requested a review from a team as a code owner December 19, 2023 02:18
@openverse-bot openverse-bot added 🟧 priority: high Stalls work on the project or its dependents 🛠 goal: fix Bug fix 💻 aspect: code Concerns the software code in the repository 🚦 status: awaiting triage Has not been triaged & therefore, not ready for work labels Dec 19, 2023
@github-actions github-actions bot added the 🧱 stack: api Related to the Django API label Dec 19, 2023
Copy link
Collaborator

@AetherUnbound AetherUnbound left a comment

Choose a reason for hiding this comment

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

Tested locally, looks good! Thanks for the quick fix!

@@ -272,7 +272,7 @@ def related(self, request, identifier=None, *_, **__):
raise APIException(getattr(e, "message", str(e)))
# If there are no hits in the search controller
except IndexError:
raise APIException("Could not find items.", 404)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this was supposed to be raising a 404, why wasn't it working before?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Because the second parameter isn't the status code, it's the error code: https://github.com/encode/django-rest-framework/blob/master/rest_framework/exceptions.py#L99-L114. Typically this is something like a slug that identifies the error type within the status code: https://github.com/encode/django-rest-framework/blob/master/rest_framework/exceptions.py#L183-L192. This lets you have multiple distinct error types within a single semantic HTTP status code that are identifiable on the client without needing to parse the potentially arbitrary error message string.

Copy link
Collaborator

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -272,7 +272,7 @@ def related(self, request, identifier=None, *_, **__):
raise APIException(getattr(e, "message", str(e)))
# If there are no hits in the search controller
except IndexError:
raise APIException("Could not find items.", 404)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Because the second parameter isn't the status code, it's the error code: https://github.com/encode/django-rest-framework/blob/master/rest_framework/exceptions.py#L99-L114. Typically this is something like a slug that identifies the error type within the status code: https://github.com/encode/django-rest-framework/blob/master/rest_framework/exceptions.py#L183-L192. This lets you have multiple distinct error types within a single semantic HTTP status code that are identifiable on the client without needing to parse the potentially arbitrary error message string.

@@ -272,7 +272,7 @@ def related(self, request, identifier=None, *_, **__):
raise APIException(getattr(e, "message", str(e)))
# If there are no hits in the search controller
except IndexError:
raise APIException("Could not find items.", 404)
raise NotFound
Copy link
Collaborator

Choose a reason for hiding this comment

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

TIL raise accepts a class object rather than an instance for lazy evaluation of the parameterless constructor 😮 https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement

It must be either a subclass or an instance of BaseException. If it is a class, the exception instance will be obtained when needed by instantiating the class with no arguments.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Woah, TIL too!

@dhruvkb dhruvkb merged commit d8a60ad into main Dec 19, 2023
66 of 70 checks passed
@dhruvkb dhruvkb changed the title Raise 404 instead of 500 exception when media not found Return status 404 instead of 500 when media not found Dec 19, 2023
@dhruvkb dhruvkb deleted the bad_related branch December 19, 2023 19:22
@obulat obulat removed the 🚦 status: awaiting triage Has not been triaged & therefore, not ready for work label Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix 🟧 priority: high Stalls work on the project or its dependents 🧱 stack: api Related to the Django API
Projects
Archived in project
5 participants