Line data Source code
1 : import {
2 : INSUFFICIENT_CREDITS_CATALOG_ID,
3 : SIGN_IN_REQUIRED_CATALOG_ID,
4 : SUBSCRIPTION_REQUIRED_CATALOG_ID,
5 : SUBSCRIPTION_UPGRADE_REQUIRED_CATALOG_ID,
6 : WORKSPACE_INSUFFICIENT_CREDITS_CATALOG_ID
7 : } from './catalogIds'
8 : import type { RuntimeErrorInfo } from './runtimeErrorMatcher'
9 : import { resolveRuntimeCatalogMatch } from './runtimeErrorMatcher'
10 :
11 : // Account preconditions are gating states (sign-in, subscription, credits) that
12 : // must open their own modal instead of surfacing as a workflow error. They are
13 : // excluded from the error panel and the error count.
14 : export type AccountPrecondition = 'sign_in' | 'subscription' | 'credits'
15 :
16 2356 : const CATALOG_ID_TO_PRECONDITION = new Map<string, AccountPrecondition>([
17 : [SIGN_IN_REQUIRED_CATALOG_ID, 'sign_in'],
18 : [SUBSCRIPTION_REQUIRED_CATALOG_ID, 'subscription'],
19 : [SUBSCRIPTION_UPGRADE_REQUIRED_CATALOG_ID, 'subscription'],
20 : [INSUFFICIENT_CREDITS_CATALOG_ID, 'credits'],
21 : [WORKSPACE_INSUFFICIENT_CREDITS_CATALOG_ID, 'credits']
22 : ])
23 :
24 59 : export function preconditionForCatalogId(
25 : catalogId: string | undefined
26 : ): AccountPrecondition | undefined {
27 59 : if (!catalogId) return undefined
28 3 : return CATALOG_ID_TO_PRECONDITION.get(catalogId)
29 : }
30 :
31 : export function isAccountPreconditionCatalogId(
32 : catalogId: string | undefined
33 : ): boolean {
34 : return preconditionForCatalogId(catalogId) !== undefined
35 : }
36 :
37 : // Classifies a single runtime error payload into the account precondition it
38 : // represents, or `undefined` when it is an ordinary workflow error.
39 59 : export function resolveAccountPrecondition(
40 : info: RuntimeErrorInfo
41 : ): AccountPrecondition | undefined {
42 : const match = resolveRuntimeCatalogMatch(info)
43 59 : return preconditionForCatalogId(match?.catalogId)
44 : }
45 :
46 : /** Cloud's run refusal says the account holds credits a plan would unlock. */
47 1 : export function promptErrorHasLockedCredits(error: unknown): boolean {
48 1 : return (
49 : error !== null &&
50 : typeof error === 'object' &&
51 : 'credits_locked' in error &&
52 : error.credits_locked === true
53 : )
54 : }
|