Skip to content

fix(plugin-cloud-storage): propagate custom hook errors during upload#16592

Merged
jacobsfletch merged 5 commits into
mainfrom
fix/cloud-storage-custom-hooks
May 20, 2026
Merged

fix(plugin-cloud-storage): propagate custom hook errors during upload#16592
jacobsfletch merged 5 commits into
mainfrom
fix/cloud-storage-custom-hooks

Conversation

@jacobsfletch

@jacobsfletch jacobsfletch commented May 12, 2026

Copy link
Copy Markdown
Member

Closes #16573.

When a collection using @payloadcms/plugin-cloud-storage has a custom afterChange hook that throws an error, the plugin swallows the error, logging it as a warn. Payload silently rolls back the changes, leaving S3 and the database in inconsistent states without notifying the user that an error has occurred.

Problem

Two problems in the plugin's afterChange handler:

  1. The inner req.payload.update() was wrapped in a try/catch that only emitted logger.warn. Anything thrown downstream — including a custom afterChange hook — was swallowed, so the outer operation completed "successfully" while its side effects were broken.
  2. The old files were deleted from cloud storage before the new file was uploaded. If anything threw mid-flight, e.g. from a user-defined custom hook, the old files were already gone and there was no recovery path.

Solution

  • Remove the swallowing try/catch around the inner update. The outer try/catch in the same hook already logs and re-throws, so errors now surface to the caller and the admin UI.
  • Wrap the skipCloudStorage context flag in try/finally so it is always cleared, even when the inner update throws.
  • Reorder operations: upload new files → persist adapter metadata → delete previous files. The deletion step now also skips any filename that matches the new doc's filename, so reuploads that reuse a key don't delete the freshly uploaded object.

Now, custom hook errors are surfaced to the caller, the previous file stays in S3, and the document keeps pointing at a valid object.


@github-actions

github-actions Bot commented May 12, 2026

Copy link
Copy Markdown
Contributor

📦 esbuild Bundle Analysis for payload

This analysis was generated by esbuild-bundle-analyzer. 🤖
This PR introduced no changes to the esbuild bundle! 🙌

@jacobsfletch jacobsfletch changed the title fix(plugin-cloud-storage): propagate user afterChange hook errors during upload fix(plugin-cloud-storage): propagate custom hook errors during upload May 15, 2026
@jacobsfletch jacobsfletch merged commit 4f0d847 into main May 20, 2026
323 of 326 checks passed
@jacobsfletch jacobsfletch deleted the fix/cloud-storage-custom-hooks branch May 20, 2026 12:46
jacobsfletch added a commit that referenced this pull request May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

plugin-cloud-storage: silent data loss when user afterChange hook throws during upload

2 participants