Line data Source code
1 : import type { BillingTelemetryFailure } from '@comfyorg/account-core/billing'
2 :
3 : import { WorkspaceApiError } from '@/platform/remote/ingest/ingestApiError'
4 : import { AuthStoreError } from '@/stores/authStore'
5 :
6 2356 : export class BillingFailureError extends Error {
7 : constructor(
8 : message: string,
9 0 : public readonly failure: BillingTelemetryFailure
10 : ) {
11 0 : super(message)
12 0 : this.name = 'BillingFailureError'
13 : }
14 : }
15 :
16 : export class PaymentPopupBlockedError extends BillingFailureError {
17 : constructor(message: string) {
18 : super(message, {
19 : failure_category: 'redirect',
20 : error_code: 'payment_popup_blocked'
21 : })
22 : this.name = 'PaymentPopupBlockedError'
23 : }
24 : }
25 :
26 0 : export function describeBillingFailure(err: unknown): BillingTelemetryFailure {
27 0 : return err instanceof BillingFailureError
28 : ? err.failure
29 : : { failure_category: categorizeBillingApiError(err) }
30 : }
31 :
32 : /**
33 : * A `WorkspaceApiError` that names its category keeps it. Otherwise a
34 : * `WorkspaceApiError`/`AuthStoreError` with no `status` never reached the
35 : * backend (`network`); a `TypeError` naming fetch/network/load is what `fetch`
36 : * throws for connectivity failures.
37 : */
38 1 : export function categorizeBillingApiError(
39 : err: unknown
40 : ): BillingTelemetryFailure['failure_category'] {
41 1 : if (err instanceof BillingFailureError) return err.failure.failure_category
42 1 : if (err instanceof WorkspaceApiError) {
43 0 : if (err.failureCategory) return err.failureCategory
44 0 : return err.status === undefined ? 'network' : 'api_rejected'
45 : }
46 1 : if (err instanceof AuthStoreError) {
47 0 : return err.status === undefined ? 'network' : 'api_rejected'
48 : }
49 1 : if (
50 : err instanceof TypeError &&
51 : /fetch|network|load failed/i.test(err.message)
52 : ) {
53 0 : return 'network'
54 : }
55 1 : return 'unknown'
56 : }
|