From 6e101a68480d7fba729d363770d3eaf5ba04d984 Mon Sep 17 00:00:00 2001 From: Twilight <46562212+twlite@users.noreply.github.com> Date: Wed, 30 Oct 2024 18:39:36 +0545 Subject: [PATCH] refactor(open-customer-portal): remove redundant error handling --- frontend/actions/open-customer-portal.ts | 30 ++++++++++-------------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/frontend/actions/open-customer-portal.ts b/frontend/actions/open-customer-portal.ts index b81de276..d7dbeef1 100644 --- a/frontend/actions/open-customer-portal.ts +++ b/frontend/actions/open-customer-portal.ts @@ -14,26 +14,20 @@ export type responseAction = { const billingUrl = absoluteUrl('/'); export async function openCustomerPortal(userStripeId: string): Promise { - let redirectUrl: string = ''; + const session = await auth(); - try { - const session = await auth(); - - if (!session?.user || !session?.user.email) { - throw new Error('Unauthorized'); - } + if (!session?.user?.email) { + throw new Error('Unauthorized'); + } - if (userStripeId) { - const stripeSession = await stripe.billingPortal.sessions.create({ - customer: userStripeId, - return_url: billingUrl, - }); + if (userStripeId) { + const stripeSession = await stripe.billingPortal.sessions.create({ + customer: userStripeId, + return_url: billingUrl, + }); - redirectUrl = stripeSession.url as string; - } - } catch (error) { - throw new Error('Failed to generate user stripe session'); + redirect(stripeSession.url as string); } - - redirect(redirectUrl); + + throw new Error('Failed to generate user stripe session'); }