Skip to content

fix(billing): re-throw on transient revenue_events INSERT failure (closes #3693) - #3697

Merged
bokelley merged 1 commit into
mainfrom
bokelley/revenue-events-throw
Apr 30, 2026
Merged

bokelley merged 1 commit into
mainfrom
bokelley/revenue-events-throw

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

Closes #3693 (the first of three triage follow-ups from #3691).

Two webhook handlers were swallowing all errors when inserting into revenue_events and returning 200 — Stripe never retried, so transient DB errors silently lost paid revenue. Now distinguishes legitimate Stripe-retry duplicates (PG 23505 unique_violation) from real transient failures (re-throws so Stripe retries with backoff).

Sites fixed

  • http.ts:4180-4239 — invoice.paid revenue_events INSERT
  • http.ts:4400-4458 — invoice.payment_failed revenue_events INSERT
} catch (revenueError) {
-  logger.error({...}, 'Failed to insert revenue event');
-  // Continue processing - don't fail the webhook
+  if ((revenueError as { code?: string })?.code === '23505') {
+    logger.info(...,'duplicate event ignored');
+  } else {
+    logger.error({...}, 'Failed to insert revenue event — re-throwing so Stripe retries');
+    throw revenueError;
+  }
}

Out of scope (follow-up): refund site

The third site at http.ts:4480 (charge.refunded) uses stripe_charge_id which is NOT a UNIQUE column on revenue_events. The 23505 pattern is unsafe there until a schema migration adds the constraint (recommend partial index UNIQUE (stripe_charge_id) WHERE revenue_type = 'refund').

Kept the swallow with a comment explaining the gap. Per the security-reviewer's M3 finding, refunds older than Stripe's listable window never reconcile via the existing backfill admin tool — the refund-side schema follow-up is needed eventually.

Test plan

  • npx tsc --noEmit clean
  • Existing admin-sync-revenue-backfill integration tests still pass (4/4)
  • Pre-commit hook (unit + dynamic imports + typecheck) passes
  • Manual: trigger an invoice.paid webhook against a customer; assert 200; trigger again with same event.id; assert 200 + only one revenue_events row (UNIQUE caught)
  • Manual: simulate a transient pool error during the INSERT; assert 5xx + Stripe retries

Refs

🤖 Generated with Claude Code

)

Two webhook handlers were swallowing all errors when inserting into
revenue_events and returning 200 — Stripe never retried, so transient
DB errors (statement timeout, connection blip, brief contention)
silently lost paid revenue. The UNIQUE constraint on stripe_invoice_id
makes the legitimate-retry case safe (PG code 23505 = duplicate
Stripe re-fire), but the swallow was also masking real transient
failures.

Now distinguishes by error code:
- 23505 (unique_violation): swallow — Stripe re-fired the same
  invoice.paid event, the row already exists
- Anything else: re-throw — let Stripe retry with backoff

Applied at:
- http.ts:4180-4239 — invoice.paid revenue_events INSERT
- http.ts:4400-4458 — invoice.payment_failed revenue_events INSERT

The third site (charge.refunded at http.ts:4480) uses stripe_charge_id
which is NOT a UNIQUE column on revenue_events. The 23505 pattern is
unsafe there until a schema migration adds the constraint. Kept the
swallow with a comment explaining the gap and pointing at the
follow-up scope (add UNIQUE (stripe_charge_id) WHERE revenue_type =
'refund' partial index, then apply the same pattern).

Per security-reviewer's framing on #3691: "audit-sync-revenue-backfill
is a 90-day workaround that requires running it; refunds older than
that window never reconcile." This closes the gap for invoice-based
revenue events; refund-side gap remains in the follow-up.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit ace36b3 into main Apr 30, 2026
13 checks passed
@bokelley
bokelley deleted the bokelley/revenue-events-throw branch April 30, 2026 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

billing: revenue_events silent-swallow loses transient-failure rows (use ON CONFLICT + re-throw)

1 participant