Line data Source code
1 : import type { BillingResult } from '@comfyorg/account-core/billing'
2 :
3 : import type { BillingApiOperation } from '@comfyorg/billing-core'
4 : import { WorkspaceApiError } from '@/platform/remote/ingest/ingestApiError'
5 :
6 : /**
7 : * A read on the SDK rail, in the shape the legacy path throws in: the HTTP
8 : * status rides along when the failure came from a response, so a caller that
9 : * treats a 401 or 403 as a denial reads it the same on either rail.
10 : * `operation` names the legacy method the read replaces, so both rails share
11 : * one Sentry group per endpoint.
12 : * `SUPERSEDED` is not a failure: the scope moved on under the read, so there
13 : * is nothing to publish and nothing to report, the same outcome as a stale
14 : * legacy read.
15 : */
16 0 : export async function readOnRail<T>(
17 : read: () => Promise<BillingResult<T>>,
18 : operation: BillingApiOperation
19 : ): Promise<T | undefined> {
20 0 : const result = await read()
21 0 : if (result.status === 'ok') return result.value
22 0 : if (result.code === 'SUPERSEDED') return undefined
23 0 : throw new WorkspaceApiError(
24 : result.code,
25 : result.httpStatus,
26 : result.code,
27 : undefined,
28 : operation
29 : )
30 : }
|