diff --git a/src/main/billing/billing-client.test.ts b/src/main/billing/billing-client.test.ts index c7009459464..c295803934b 100644 --- a/src/main/billing/billing-client.test.ts +++ b/src/main/billing/billing-client.test.ts @@ -80,6 +80,12 @@ describe('billing client', () => { await expect(mintBillingToken(endpoints, 'a', null, fetchImpl)).rejects.toBeInstanceOf(BillingOrgMismatchError) }) + it('treats 403 not_a_member as a team-switch error, not a session failure', async () => { + const fetchImpl: typeof fetch = async () => + new Response(JSON.stringify({ error: 'not_a_member' }), { status: 403 }) + await expect(mintBillingToken(endpoints, 'a', 'org_gone', fetchImpl)).rejects.toBeInstanceOf(BillingOrgMismatchError) + }) + it('falls back to the empty body on an older auth (400), still checking the claim', async () => { const bodies: Array = [] const fetchImpl: typeof fetch = async (_input, init) => { diff --git a/src/main/billing/billing-client.ts b/src/main/billing/billing-client.ts index 8ef57a07839..c38521d3b35 100644 --- a/src/main/billing/billing-client.ts +++ b/src/main/billing/billing-client.ts @@ -103,7 +103,13 @@ export async function mintBillingToken( response = await post('{}') } if (!response.ok) { - throw new OrcaCloudRequestError(response.status, (await readErrorCode(response)) ?? undefined) + const code = (await readErrorCode(response)) ?? undefined + if (response.status === 403 && orgId && code === 'not_a_member') { + // This device shows a team the person is no longer on: a switch error, not a session one + // (refreshing the session would not help). + throw new BillingOrgMismatchError(orgId) + } + throw new OrcaCloudRequestError(response.status, code) } const body = TokenBody.safeParse(await response.json()) if (!body.success) {