From 25687bb497817a99e09082eb4afc02f9f6232356 Mon Sep 17 00:00:00 2001 From: giancarlo Date: Sun, 28 Apr 2024 01:04:55 +0700 Subject: [PATCH] Handle redirect errors in routes and actions The commit introduces a check for redirect errors in routes and actions modules to provide better error handling. It imports `isRedirectError` from 'next/dist/client/components/redirect' and uses it to check if an error is a redirect error, in which case re-throws the error. A minor modification was also made in the creation of the team account's home path variable. --- .../src/server/actions/create-team-account-server-actions.ts | 4 ++-- packages/next/src/actions/index.ts | 5 +++++ packages/next/src/routes/index.ts | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/features/team-accounts/src/server/actions/create-team-account-server-actions.ts b/packages/features/team-accounts/src/server/actions/create-team-account-server-actions.ts index 4756eb92e..c86b7f1e0 100644 --- a/packages/features/team-accounts/src/server/actions/create-team-account-server-actions.ts +++ b/packages/features/team-accounts/src/server/actions/create-team-account-server-actions.ts @@ -10,7 +10,7 @@ import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-clie import { CreateTeamSchema } from '../../schema/create-team.schema'; import { createCreateTeamAccountService } from '../services/create-team-account.service'; -const TEAM_ACCOUNTS_HOME_PATH = z +const path = z .string({ required_error: 'variable TEAM_ACCOUNTS_HOME_PATH is required', }) @@ -31,7 +31,7 @@ export const createOrganizationAccountAction = enhanceAction( throw new Error('Error creating team account'); } - const accountHomePath = TEAM_ACCOUNTS_HOME_PATH + '/' + data.slug; + const accountHomePath = path + '/' + data.slug; redirect(accountHomePath); }, diff --git a/packages/next/src/actions/index.ts b/packages/next/src/actions/index.ts index c71e13e0d..7959e36b0 100644 --- a/packages/next/src/actions/index.ts +++ b/packages/next/src/actions/index.ts @@ -1,5 +1,6 @@ import 'server-only'; +import { isRedirectError } from 'next/dist/client/components/redirect'; import { redirect } from 'next/navigation'; import type { User } from '@supabase/supabase-js'; @@ -81,6 +82,10 @@ export function enhanceAction< // pass the data to the action return await fn(data, user); } catch (error) { + if (isRedirectError(error)) { + throw error; + } + // capture the exception await captureException(error); diff --git a/packages/next/src/routes/index.ts b/packages/next/src/routes/index.ts index c2bee0830..e8a394ba5 100644 --- a/packages/next/src/routes/index.ts +++ b/packages/next/src/routes/index.ts @@ -1,5 +1,6 @@ import 'server-only'; +import { isRedirectError } from 'next/dist/client/components/redirect'; import { redirect } from 'next/navigation'; import { NextRequest, NextResponse } from 'next/server'; @@ -102,6 +103,10 @@ export const enhanceRouteHandler = < try { return await handler({ request, body, user }); } catch (error) { + if (isRedirectError(error)) { + throw error; + } + // capture the exception await captureException(error);