:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(billing): handle oversized usage breakdowns (#86265)
Pawel Cebula56ceac710dd5
diff --git a/frontend/src/scenes/billing/BillingUsage.tsx b/frontend/src/scenes/billing/BillingUsage.tsxindex 802ee70ce57b..944bb2135c1e 100644--- a/frontend/src/scenes/billing/BillingUsage.tsx+++ b/frontend/src/scenes/billing/BillingUsage.tsx@@ -8,6 +8,7 @@ import { LemonButton, LemonCheckbox, LemonSelect } from '@posthog/lemon-ui'import { DateFilter } from 'lib/components/DateFilter/DateFilter'import { exportsLogic } from 'lib/components/ExportButton/exportsLogic'import { RestrictionScope, useRestrictedArea } from 'lib/components/RestrictedArea'+import { LemonBanner } from 'lib/lemon-ui/LemonBanner'import { LemonInputSelect } from 'lib/lemon-ui/LemonInputSelect/LemonInputSelect'import { LemonLabel } from 'lib/lemon-ui/LemonLabel/LemonLabel'import { Tooltip } from 'lib/lemon-ui/Tooltip'@@ -38,6 +39,7 @@ export function BillingUsage(): JSX.Element {dateFrom,dateTo,billingUsageResponseLoading,+ billingUsageError,dateOptions,excludeEmptySeries,finalHiddenSeries,@@ -207,6 +209,8 @@ export function BillingUsage(): JSX.Element {</div></div>+ {billingUsageError && <LemonBanner type="warning">{billingUsageError.detail}</LemonBanner>}+{showSeries && (<BillingLineGraphseries={series}diff --git a/frontend/src/scenes/billing/billingUsageLogic.test.ts b/frontend/src/scenes/billing/billingUsageLogic.test.tsindex b191aff6ea14..b9e851c5c816 100644--- a/frontend/src/scenes/billing/billingUsageLogic.test.ts+++ b/frontend/src/scenes/billing/billingUsageLogic.test.ts@@ -1,4 +1,20 @@-import { BillingUsageResponse, BillingUsageResponseBreakdownType, convertDesktopUsageSeries } from './billingUsageLogic'+import { expectLogic } from 'kea-test-utils'++import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'+import { billingLogic } from 'scenes/billing/billingLogic'++import { billingJson } from '~/mocks/fixtures/_billing'+import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'++import {+ BILLING_USAGE_QUERY_TOO_LARGE_CODE,+ BillingUsageResponse,+ BillingUsageResponseBreakdownType,+ convertDesktopUsageSeries,+ getBillingUsageError,+ billingUsageLogic,+} from './billingUsageLogic'const series = (label: string, usageType: string, data: number[]): BillingUsageResponse['results'][number] => ({id: 1,@@ -58,3 +74,66 @@ describe('convertDesktopUsageSeries', () => {})})})++describe('getBillingUsageError', () => {+ it('preserves the actionable query-size error', () => {+ expect(+ getBillingUsageError({+ code: BILLING_USAGE_QUERY_TOO_LARGE_CODE,+ detail: 'Select a product.',+ })+ ).toEqual({+ code: BILLING_USAGE_QUERY_TOO_LARGE_CODE,+ detail: 'Select a product.',+ })+ })++ it('ignores errors without the expected API shape', () => {+ expect(getBillingUsageError(new Error('request failed'))).toBeNull()+ expect(getBillingUsageError({ code: BILLING_USAGE_QUERY_TOO_LARGE_CODE })).toBeNull()+ })+})++describe('billingUsageLogic loader', () => {+ let logic: ReturnType<typeof billingUsageLogic.build>+ let toastErrorSpy: jest.SpyInstance++ beforeEach(() => {+ initKeaTests()+ toastErrorSpy = jest.spyOn(lemonToast, 'error').mockImplementation(() => ({ id: 'x' }) as any)+ })++ afterEach(() => {+ logic?.unmount()+ toastErrorSpy.mockRestore()+ })++ it('handles query-size errors without failing the loader', async () => {+ useMocks({+ get: {+ '/api/billing': () => [200, billingJson],+ '/api/billing/usage/': () => [+ 400,+ { code: BILLING_USAGE_QUERY_TOO_LARGE_CODE, detail: 'Select a product.' },+ ],+ },+ })++ billingLogic.mount()+ await expectLogic(billingLogic, () => billingLogic.actions.loadBilling()).toFinishAllListeners()++ logic = billingUsageLogic()+ logic.mount()++ await expectLogic(logic)+ .toDispatchActions(['loadBillingUsageSuccess'])+ .toNotHaveDispatchedActions(['loadBillingUsageFailure'])+ .toFinishAllListeners()++ expect(logic.values.billingUsageError).toEqual({+ code: BILLING_USAGE_QUERY_TOO_LARGE_CODE,+ detail: 'Select a product.',+ })+ expect(toastErrorSpy).not.toHaveBeenCalled()+ })+})diff --git a/frontend/src/scenes/billing/billingUsageLogic.ts b/frontend/src/scenes/billing/billingUsageLogic.tsindex 44723e65e874..2e4fa119ad98 100644--- a/frontend/src/scenes/billing/billingUsageLogic.ts+++ b/frontend/src/scenes/billing/billingUsageLogic.ts@@ -102,6 +102,24 @@ export interface BillingUsageLogicProps {syncWithUrl?: boolean // Default false - only intended on usage and spend pages}+export const BILLING_USAGE_QUERY_TOO_LARGE_CODE = 'usage_breakdown_too_large'++export interface BillingUsageError {+ code: string+ detail: string+}++export const getBillingUsageError = (error: unknown): BillingUsageError | null => {+ if (!error || typeof error !== 'object') {+ return null+ }++ const candidate = error as { code?: unknown; detail?: unknown }+ return typeof candidate.code === 'string' && typeof candidate.detail === 'string'+ ? { code: candidate.code, detail: candidate.detail }+ : null+}+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface billingUsageLogicValues {billing: BillingType | null // billingLogic@@ -110,6 +128,7 @@ export interface billingUsageLogicValues {currentOrganization: OrganizationType | null // billingLogicisHobby: boolean // preflightLogicbillingPeriodMarkers: BillingPeriodMarker[]+ billingUsageError: BillingUsageError | nullbillingUsageResponse: BillingUsageResponse | nullbillingUsageResponseLoading: booleandateFrom: string@@ -167,6 +186,9 @@ export interface billingUsageLogicActions {resetFilters: () => {value: true}+ setBillingUsageError: (error: BillingUsageError | null) => {+ error: BillingUsageError | null+ }setDateRange: (dateFrom: string | null,dateTo: string | null,@@ -244,9 +266,14 @@ export interface billingUsageLogicMeta {dates: string[]id: numberlabel: string- }[]+ }[],+ billingUsageError: BillingUsageError | null+ ) => boolean+ showEmptyState: (+ showSeries: boolean,+ billingUsageResponse: BillingUsageResponse | null,+ billingUsageError: BillingUsageError | null) => boolean- showEmptyState: (showSeries: boolean, billingUsageResponse: BillingUsageResponse | null) => booleanheading: (filters: {breakdowns?: ('team' | 'type')[] | undefinedinterval?: 'day' | 'month' | 'week' | undefined@@ -299,8 +326,9 @@ export const billingUsageLogic = kea<billingUsageLogicType>([setExcludeEmptySeries: (exclude: boolean, shouldDebounce: boolean = true) => ({ exclude, shouldDebounce }),toggleTeamBreakdown: true,resetFilters: true,+ setBillingUsageError: (error: BillingUsageError | null) => ({ error }),}),- loaders(({ values }) => ({+ loaders(({ values, actions }) => ({billingUsageResponse: [null as BillingUsageResponse | null,{@@ -308,6 +336,7 @@ export const billingUsageLogic = kea<billingUsageLogicType>([if (!values.canViewUsageAndSpend || values.isHobby) {return null}+ actions.setBillingUsageError(null)const { usage_types, team_ids, breakdowns, interval } = values.filtersconst params = {...(usage_types && usage_types.length > 0 ? { usage_types: JSON.stringify(usage_types) } : {}),@@ -320,8 +349,15 @@ export const billingUsageLogic = kea<billingUsageLogicType>([try {return await api.get(`api/billing/usage/?${toParams(params)}`)} catch (error) {- lemonToast.error('Failed to load billing usage. Please try again or contact support.')- throw error+ const billingUsageError = getBillingUsageError(error)+ actions.setBillingUsageError(+ billingUsageError?.code === BILLING_USAGE_QUERY_TOO_LARGE_CODE ? billingUsageError : null+ )+ if (billingUsageError?.code !== BILLING_USAGE_QUERY_TOO_LARGE_CODE) {+ lemonToast.error('Failed to load billing usage. Please try again or contact support.')+ throw error+ }+ return null}},},@@ -372,6 +408,12 @@ export const billingUsageLogic = kea<billingUsageLogicType>([resetFilters: () => false,},],+ billingUsageError: [+ null as BillingUsageError | null,+ {+ setBillingUsageError: (_, { error }) => error,+ },+ ],})),selectors({dateOptions: [@@ -437,14 +479,20 @@ export const billingUsageLogic = kea<billingUsageLogicType>([excludeEmptySeries ? Array.from(new Set([...userHiddenSeries, ...emptySeriesIDs])) : userHiddenSeries,],showSeries: [- (s) => [s.billingUsageResponseLoading, s.series],- (billingUsageResponseLoading: boolean, series: billingUsageLogicType['values']['series']) =>- billingUsageResponseLoading || series.length > 0,+ (s) => [s.billingUsageResponseLoading, s.series, s.billingUsageError],+ (+ billingUsageResponseLoading: boolean,+ series: billingUsageLogicType['values']['series'],+ billingUsageError: BillingUsageError | null+ ) => billingUsageResponseLoading || (!billingUsageError && series.length > 0),],showEmptyState: [- (s) => [s.showSeries, s.billingUsageResponse],- (showSeries: boolean, billingUsageResponse: BillingUsageResponse | null) =>- !showSeries && !!billingUsageResponse,+ (s) => [s.showSeries, s.billingUsageResponse, s.billingUsageError],+ (+ showSeries: boolean,+ billingUsageResponse: BillingUsageResponse | null,+ billingUsageError: BillingUsageError | null+ ) => !showSeries && !!billingUsageResponse && !billingUsageError,],heading: [(s) => [s.filters],