- Notifications
You must be signed in to change notification settings - Fork 3.2k
fix(storage-s3): return error status 404 when file is not found instead of 500 #11733
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
Conversation
| @denolfe Tagging you as you previously reviewed #10327 for storage-vercel-blob There is also PR with same fix for storage-azure #11734 |
| @denolfe I have now updated this PR with latest upstream changes and done proper testing of this PR. Would you please consider it? To test I built the storage-s3 package using pack_to_dest.sh script and installed that tgz package in a repo of mine. Here are the results. With stock 3.42.0 HTTP error response code is 500: Log in console: With local storage-s3 package with this change HTTP error response code is 404: Log in console: |
| I of course testing the regular case of downloading images that did exist as well, and it's also working as expected with the change. |
paulpopus left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's expand the int tests to check the returned error code and to make sure a file that does exist returns as expected
<!-- Thank you for the PR! Please go through the checklist below and make sure you've completed all the steps. Please review the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository if you haven't already. The following items will ensure that your PR is handled as smoothly as possible: - PR Title must follow conventional commits format. For example, `feat: my new feature`, `fix(plugin-seo): my fix`. - Minimal description explained as if explained to someone not immediately familiar with the code. - Provide before/after screenshots or code diffs if applicable. - Link any related issues/discussions from GitHub or Discord. - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Fixes # --> ### What? In a similar vein to #11734, #11733, #10327 - this PR returns a 404 in the response when a file is not found while using the `storage-gcs` adapter. Currently a 500 is returned. ### Why? To return the correct error level in the response when a file is not found when using `storage-gcs`. ### How? The GCS nodejs library exposes the `ApiError` as a general error - these changes check that the caught error is an instance of this class and if the provided code is a `404`.
| 🚀 This is included in version v3.43.0 |



What?
The s3 storage adapter returns a 500 internal server error when a file is not found.
It's expected that it will return 404 when a file is not found.
Why?
The getObject function from aws s3 sdk does not return undefined when a blob is not found, but throws a NoSuchKey error: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-s3/Class/NoSuchKey/
How?
Check if exception thrown is of type NoSuchKey and return a 404 in that case.
Related discord discussion:
https://discord.com/channels/967097582721572934/1350826594062696539/1350826594062696539