catalyst icon indicating copy to clipboard operation
catalyst copied to clipboard

NEXT_REDIRECT error thrown when getting BigCommerceAuthError in try/catch

Open oliverdowie opened this issue 5 months ago • 1 comments

Describe the bug The onError callback in the GraphQL client is designed to detect errors of the BigCommerceAuthError type and redirect to the auth signout route. This ensures that if a user's session is invalid we trigger a full sign out to clear any user data, and redirect to the login page.

onError: (error, queryType) => {
    if (error instanceof BigCommerceAuthError && queryType === 'query') {
      redirect('/api/auth/signout');
    }
  }

This works great for simple functions like getCart. However, we have noticed that if the query/mutation to BC is inside a try/catch, we instead get a NEXT_REDIRECT error.

I believe this is actually expected behaviour from Next. NextJS Redirect Behaviour

redirect throws an error so it should be called outside the try block when using try/catch statements.

In the case of the addToCart function, the call to BC is in a try block. The GraphQL client will get the BigCommerceAuthError and trigger the redirect. However, the redirect will throw an error, which is then caught by the catch block, preventing completion of the redirect.

To Reproduce Steps to reproduce the behavior:

  1. Sign in to Catalyst storefront
  2. Open Catalyst in a private window or another device and sign in again.
  3. This will invalidate the first session due to BC only allowing individual sessions.
  4. On the first window/device, try adding an item to the cart.
  5. See error

Expected behavior The expectation is that for any query/mutation to BC, the automatic redirect to the signout route should be successful.

Screenshots

Image

oliverdowie avatar Sep 09 '25 05:09 oliverdowie

This upcoming feature in NextJS might help solve this: unstable_rethrow

oliverdowie avatar Sep 10 '25 05:09 oliverdowie