Fixed gift preview redirect for invalid tokens#27645
Conversation
no ref `giftService.getByToken` resolves with `null` (not a thrown error) when the token does not exist, so the gift preview controller's `try/catch` never caught the missing-gift case. The handler then dereferenced `gift.cadence`, producing an unhandled rejection and a 500 response for visitors hitting a stale or mistyped `/gift/<token>` URL. Throw a `NotFoundError` inside the existing `try` block when the lookup returns `null` so the same catch logs and redirects to the homepage for both the throw and the not-found paths. Added a sibling unit test for the `null` return so we don't regress this again.
WalkthroughThe gift preview controller is updated to explicitly handle the case where a gift lookup returns no result. The controller now imports the error utilities and throws a 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
closes https://linear.app/ghost/issue/BER-3524
/gift/<invalid-token>(e.g. a stale share link or mistyped URL) crashed the request with:ERROR Unhandled rejection: Cannot read properties of null (reading 'cadence')giftService.getByTokenresolves withnullfor an unknown token rather than throwing, so the controller's existingtry/catchdid not cover the missing-gift case. The handler then dereferencedgift.cadenceand produced a 500 for visitors hitting any non-resolving gift URL.Changes
ghost/core/core/server/web/gift-preview/controller.js— whengetByTokenreturnsnull, throw aNotFoundErrorinside the existingtryblock so the samecatch(warn + 302 to homepage) handles both the throw and the not-found paths. No new branch, no duplicated redirect logic.ghost/core/test/unit/server/web/gift-preview/controller.test.js— added a regression test that mocksgetByTokento resolvenulland asserts the homepage redirect.Test plan
nullreturn pathpnpm test:single test/unit/server/web/gift-preview/controller.test.jsfromghost/core/curl -i https://<host>/gift/abcreturns302to/, no unhandled rejection in logs