Repository navigation
fix(billing): resolve terminal promise even if success side effects throw #14964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c1727b2
32290ac
f7147f8
5b607f4
08893ef
50023ea
2a8ea4c
6f7ea6c
d8628de
dabc3c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import type { BillingCycle } from '@/platform/cloud/subscription/utils/subscript | |
| import { useSettingsDialog } from '@/platform/settings/composables/useSettingsDialog' | ||
| import { isCloud } from '@/platform/distribution/types' | ||
| import { useTelemetry } from '@/platform/telemetry' | ||
| import { reportError } from '@/platform/telemetry/reportError' | ||
| import type { | ||
| BillingFailure, | ||
| PaymentIntentSource, | ||
|
|
@@ -670,121 +671,130 @@ export const useBillingOperationStore = defineStore('billingOperation', () => { | |
| if (!operation) return | ||
|
|
||
| updateOperationStatus(opId, 'succeeded', null) | ||
| cleanup(opId) | ||
|
|
||
| const telemetry = useTelemetry() | ||
| const now = Date.now() | ||
| const operationDurationMs = now - operation.operationStartedAt | ||
| telemetry?.trackBillingEvent({ | ||
| operation: 'operation', | ||
| stage: 'succeeded', | ||
| outcome: 'success', | ||
| billing_op_id: opId, | ||
| operation_type: operation.type, | ||
| tier: operation.tier, | ||
| cycle: operation.cycle, | ||
| checkout_type: operation.checkoutType, | ||
| payment_intent_source: operation.paymentIntentSource, | ||
| duration_ms: operationDurationMs | ||
| }) | ||
| try { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: the The summary says it wraps "the success-path side-effects block ( I think wider is correct here: a throw from Could the summary be updated to say the whole post-status block is wrapped? And is a partial-success outcome something the callers of On coverage: the new test throws from the second
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed deliberate. The PR summary now states that the whole post-status block is protected and documents the partial-success contract: callers receive the authoritative
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deliberate, and your enumeration is exact. Re-read it at
Your poll point checks out. On whether callers need to see partial success: today, no. The only caller is Fixed the description rather than the code: it now has a "Scope of the guarded block" section naming the real boundary instead of the three tail calls, and spells out the |
||
| cleanup(opId) | ||
|
|
||
| if ( | ||
| operation.type === 'subscription' && | ||
| operation.businessAttemptStartedAt !== undefined | ||
| ) { | ||
| const durationMs = now - operation.businessAttemptStartedAt | ||
| const telemetry = useTelemetry() | ||
| const now = Date.now() | ||
| const operationDurationMs = now - operation.operationStartedAt | ||
| telemetry?.trackBillingEvent({ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (non-blocking): one
This is not a regression — |
||
| operation: 'subscription_checkout', | ||
| operation: 'operation', | ||
| stage: 'succeeded', | ||
| outcome: 'success', | ||
| billing_op_id: opId, | ||
| operation_type: operation.type, | ||
| tier: operation.tier, | ||
| cycle: operation.cycle, | ||
| checkout_type: operation.checkoutType, | ||
| payment_intent_source: operation.paymentIntentSource, | ||
| billing_op_id: opId, | ||
| duration_ms: durationMs | ||
| duration_ms: operationDurationMs | ||
| }) | ||
| // Also fires the legacy event for providers (Mixpanel, GTM) that don't | ||
| // implement trackBillingEvent. Gated to actual new/upgraded | ||
| // subscriptions — a downgrade-to-personal is churn, not a conversion, | ||
| // and this event drives a GA4 "subscription succeeded" conversion goal. | ||
| if (!operation.downgradeToPersonal) { | ||
| telemetry?.trackMonthlySubscriptionSucceeded({ | ||
|
|
||
| if ( | ||
| operation.type === 'subscription' && | ||
| operation.businessAttemptStartedAt !== undefined | ||
| ) { | ||
| const durationMs = now - operation.businessAttemptStartedAt | ||
| telemetry?.trackBillingEvent({ | ||
| operation: 'subscription_checkout', | ||
| stage: 'succeeded', | ||
| outcome: 'success', | ||
| tier: operation.tier, | ||
| cycle: operation.cycle, | ||
| checkout_type: operation.checkoutType, | ||
| payment_intent_source: operation.paymentIntentSource, | ||
| billing_op_id: opId | ||
| billing_op_id: opId, | ||
| duration_ms: durationMs | ||
| }) | ||
| // Also fires the legacy event for providers (Mixpanel, GTM) that don't | ||
| // implement trackBillingEvent. Gated to actual new/upgraded | ||
| // subscriptions — a downgrade-to-personal is churn, not a conversion, | ||
| // and this event drives a GA4 "subscription succeeded" conversion goal. | ||
| if (!operation.downgradeToPersonal) { | ||
| telemetry?.trackMonthlySubscriptionSucceeded({ | ||
| tier: operation.tier, | ||
| cycle: operation.cycle, | ||
| checkout_type: operation.checkoutType, | ||
| payment_intent_source: operation.paymentIntentSource, | ||
| billing_op_id: opId | ||
| }) | ||
| } | ||
| } else if ( | ||
| operation.type === 'topup' && | ||
| operation.businessAttemptStartedAt !== undefined | ||
| ) { | ||
| telemetry?.trackBillingEvent({ | ||
| operation: 'topup', | ||
| stage: 'succeeded', | ||
| outcome: 'success', | ||
| billing_op_id: opId, | ||
| duration_ms: now - operation.businessAttemptStartedAt | ||
| }) | ||
| } | ||
| // Mirrors handleFailure's structure: not gated on businessAttemptStartedAt, | ||
| // since a downgrade always has its own startedAt for duration_ms below. | ||
| if (operation.downgradeToPersonal) { | ||
| telemetry?.trackBillingEvent({ | ||
| operation: 'downgrade_to_personal', | ||
| stage: 'succeeded', | ||
| outcome: 'success', | ||
| member_removal_count: | ||
| operation.downgradeToPersonal.memberRemovalCount, | ||
| member_removal_failures: | ||
| operation.downgradeToPersonal.memberRemovalFailures, | ||
| target_tier: operation.downgradeToPersonal.targetTier, | ||
| duration_ms: now - operation.downgradeToPersonal.startedAt | ||
| }) | ||
| } | ||
| } else if ( | ||
| operation.type === 'topup' && | ||
| operation.businessAttemptStartedAt !== undefined | ||
| ) { | ||
| telemetry?.trackBillingEvent({ | ||
| operation: 'topup', | ||
| stage: 'succeeded', | ||
| outcome: 'success', | ||
| billing_op_id: opId, | ||
| duration_ms: now - operation.businessAttemptStartedAt | ||
| }) | ||
| } | ||
| // Mirrors handleFailure's structure: not gated on businessAttemptStartedAt, | ||
| // since a downgrade always has its own startedAt for duration_ms below. | ||
| if (operation.downgradeToPersonal) { | ||
| telemetry?.trackBillingEvent({ | ||
| operation: 'downgrade_to_personal', | ||
| stage: 'succeeded', | ||
| outcome: 'success', | ||
| member_removal_count: operation.downgradeToPersonal.memberRemovalCount, | ||
| member_removal_failures: | ||
| operation.downgradeToPersonal.memberRemovalFailures, | ||
| target_tier: operation.downgradeToPersonal.targetTier, | ||
| duration_ms: now - operation.downgradeToPersonal.startedAt | ||
| }) | ||
| } | ||
|
|
||
| const billingContext = useBillingContext() | ||
| const capabilities = useBillingCapabilities() | ||
| if (operation.type === 'subscription') { | ||
| await Promise.allSettled([ | ||
| billingContext.reconcileSubscriptionSuccess(), | ||
| capabilities.refresh() | ||
| ]) | ||
| } else { | ||
| await Promise.allSettled([ | ||
| billingContext.fetchStatus(), | ||
| billingContext.fetchBalance(), | ||
| capabilities.refresh() | ||
| ]) | ||
| } | ||
|
|
||
| if (operation.type === 'cancel') { | ||
| useTeamWorkspaceStore().updateActiveWorkspace({ isSubscribed: false }) | ||
| resolveTerminal(opId) | ||
| return | ||
| } | ||
| const billingContext = useBillingContext() | ||
| const capabilities = useBillingCapabilities() | ||
| if (operation.type === 'subscription') { | ||
| await Promise.allSettled([ | ||
| billingContext.reconcileSubscriptionSuccess(), | ||
| capabilities.refresh() | ||
| ]) | ||
| } else { | ||
| await Promise.allSettled([ | ||
| billingContext.fetchStatus(), | ||
| billingContext.fetchBalance(), | ||
| capabilities.refresh() | ||
| ]) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| // A subscription checkout shows its own success step in the pricing dialog, | ||
| // so leave it open. Top-ups have no such step: close and surface settings. | ||
| if (operation.type === 'topup') { | ||
| useDialogStore().closeDialog({ key: 'top-up-credits' }) | ||
| useSettingsDialog().show(isCloud ? 'workspace' : 'credits') | ||
| } | ||
| if (operation.type === 'cancel') { | ||
| useTeamWorkspaceStore().updateActiveWorkspace({ isSubscribed: false }) | ||
| return | ||
| } | ||
|
|
||
| const toastStore = useToastStore() | ||
| const messageKey = | ||
| operation.type === 'subscription' | ||
| ? 'billingOperation.subscriptionSuccess' | ||
| : 'billingOperation.topupSuccess' | ||
| // A subscription checkout shows its own success step in the pricing dialog, | ||
| // so leave it open. Top-ups have no such step: close and surface settings. | ||
| if (operation.type === 'topup') { | ||
| useDialogStore().closeDialog({ key: 'top-up-credits' }) | ||
| useSettingsDialog().show(isCloud ? 'workspace' : 'credits') | ||
| } | ||
|
|
||
| toastStore.add({ | ||
| severity: 'success', | ||
| summary: t(messageKey), | ||
| life: 5000 | ||
| }) | ||
| const toastStore = useToastStore() | ||
| const messageKey = | ||
| operation.type === 'subscription' | ||
| ? 'billingOperation.subscriptionSuccess' | ||
| : 'billingOperation.topupSuccess' | ||
|
|
||
| resolveTerminal(opId) | ||
| toastStore.add({ | ||
| severity: 'success', | ||
| summary: t(messageKey), | ||
| life: 5000 | ||
| }) | ||
| } catch (error) { | ||
| reportError(error, { | ||
| errorType: 'failure_handling_billing_operation_success', | ||
| context: { billing_op_id: opId } | ||
| }) | ||
| throw error | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit-pick (non-blocking): worth a one-line comment saying this rethrow is deliberately inert.
|
||
| } finally { | ||
| resolveTerminal(opId) | ||
| } | ||
| } | ||
|
|
||
| function handleFailure(opId: string, errorMessage: string | null) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (non-blocking): could this case assert
mockReportErrortoo?Its two siblings above pin both halves of the new contract (terminal resolution and the report), and this is the case where a silent regression is easiest to introduce. The throw only reaches the
catchbecause it happens while the arguments toPromise.allSettledare being evaluated; ifreconcileSubscriptionSuccessever becomes a rejected promise instead of a synchronous throw,allSettledswallows it, the terminal promise still resolvessucceeded, and this test keeps passing while the reporting path is no longer exercised at all.