Skip to content

NEXT_REDIRECT error thrown when getting BigCommerceAuthError in try/catch #2597

@oliverdowie

Description

@oliverdowie

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions