Skip to content

Commit f00eeb4

Browse files
committed
Split try catch in two
1 parent 4a6d7c0 commit f00eeb4

File tree

1 file changed

+24
-12
lines changed
  • web/src/app/api/stripe/cancel-subscription

1 file changed

+24
-12
lines changed

web/src/app/api/stripe/cancel-subscription/route.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ export async function POST() {
3030
subscription.stripe_subscription_id,
3131
{ cancel_at_period_end: true },
3232
)
33+
} catch (error: unknown) {
34+
const message =
35+
(error as { raw?: { message?: string } })?.raw?.message ||
36+
'Failed to cancel subscription in Stripe.'
37+
logger.error(
38+
{ error: message, userId, subscriptionId: subscription.stripe_subscription_id },
39+
'Stripe subscription cancellation failed',
40+
)
41+
return NextResponse.json({ error: message }, { status: 500 })
42+
}
3343

44+
try {
3445
await db
3546
.update(schema.subscription)
3647
.set({ cancel_at_period_end: true, scheduled_tier: null, updated_at: new Date() })
@@ -40,21 +51,22 @@ export async function POST() {
4051
subscription.stripe_subscription_id,
4152
),
4253
)
43-
44-
logger.info(
45-
{ userId, subscriptionId: subscription.stripe_subscription_id },
46-
'Subscription set to cancel at period end',
47-
)
48-
49-
return NextResponse.json({ success: true })
5054
} catch (error: unknown) {
51-
const message =
52-
(error as { raw?: { message?: string } })?.raw?.message ||
53-
'Internal server error canceling subscription.'
55+
const message = error instanceof Error ? error.message : String(error)
5456
logger.error(
5557
{ error: message, userId, subscriptionId: subscription.stripe_subscription_id },
56-
'Failed to cancel subscription',
58+
'Stripe subscription set to cancel but failed to update local DB — data is inconsistent',
59+
)
60+
return NextResponse.json(
61+
{ error: 'Subscription canceled but failed to update records. Please contact support.' },
62+
{ status: 500 },
5763
)
58-
return NextResponse.json({ error: message }, { status: 500 })
5964
}
65+
66+
logger.info(
67+
{ userId, subscriptionId: subscription.stripe_subscription_id },
68+
'Subscription set to cancel at period end',
69+
)
70+
71+
return NextResponse.json({ success: true })
6072
}

0 commit comments

Comments
 (0)