-
Notifications
You must be signed in to change notification settings - Fork 318
Fix NEXT_REDIRECT error when auth redirects occur in try/catch blocks #2603
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
base: canary
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for GitHub.
3 Skipped Deployments
|
🦋 Changeset detectedLatest commit: c4c5aed The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@copilot do you see an opportunity to de-duplicate this code in some common lib in the interest of DRY, or does it make more sense to keep these implementations distinct? |
Good point! I've refactored the code to use a common utility function. Created This approach provides the DRY benefits you mentioned while keeping the action-specific error handling logic (different return types, translations, etc.) distinct where it makes sense. The utility function also provides better documentation and centralized control over how we handle Next.js control flow errors. Fixed in commit f5555ef. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
@copilot can you just inline the |
1a6fcb9
to
b3cd637
Compare
@copilot I've updated your code to address @chanceaclark's feedback, please check my changes and rewrite the PR accordingly |
b3cd637
to
385b141
Compare
I've updated the implementation to inline
I've reviewed the changes you made to address @chanceaclark's feedback. The implementation looks correct - |
385b141
to
97e2e3b
Compare
…blocks Co-Authored-By: bookernath <[email protected]>
97e2e3b
to
c4c5aed
Compare
Hi @copilot and Catalyst team, I’m working on a similar solution for my clients and came across this PR. From what I understand, the main reason we need to rethrow the error is to prevent the try/catch blocks from swallowing Currently, Catalyst automatically logs users out when a query responds with BigCommerceAuthError, thanks to:
I believe we should apply the same behavior to mutations as well, which means removing If you aggree my above point, there’s an extra issue needs to be fixed: for certain mutations (e.g. |
Problem
The GraphQL client's
onError
callback is designed to detectBigCommerceAuthError
and automatically redirect to the auth signout route to handle invalid sessions:However, when GraphQL operations are wrapped in try/catch blocks (as they are in most server actions), the
redirect()
call throws aNEXT_REDIRECT
error that gets caught by the catch block, preventing the redirect from completing.This causes auth redirects to fail when users have invalid sessions, particularly in scenarios like:
Root Cause
According to Next.js documentation,
redirect()
throws an error and should be called outside try blocks. When the GraphQL client'sonError
callback triggers a redirect during a fetch operation inside a try block, the resultingNEXT_REDIRECT
error is caught and handled as a regular error.Solution
This PR implements the recommended Next.js solution using
unstable_rethrow
to properly handle Next.js control flow errors in catch blocks.The fix directly imports and calls
unstable_rethrow
in each server action's catch block to ensure Next.js control flow errors (likeNEXT_REDIRECT
) are properly re-thrown:Files Modified
Updated 10 server action files that handle authenticated operations:
Cart Operations:
core/app/[locale]/(default)/product/[slug]/_actions/add-to-cart.tsx
core/app/[locale]/(default)/compare/_actions/add-to-cart.tsx
core/app/[locale]/(default)/account/wishlists/[id]/_actions/add-to-cart.tsx
core/app/[locale]/(default)/cart/_actions/update-line-item.ts
Account Management:
core/app/[locale]/(default)/account/settings/_actions/update-customer.ts
core/app/[locale]/(default)/account/settings/_actions/change-password.ts
core/app/[locale]/(default)/account/addresses/_actions/create-address.ts
Wishlist Operations:
core/app/[locale]/(default)/account/wishlists/_actions/new-wishlist.ts
core/app/[locale]/(default)/account/wishlists/_actions/remove-wishlist-item.ts
core/app/[locale]/(default)/product/[slug]/_actions/wishlist-action.ts
Impact
Fixes #2597
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.