返回 AiToEarn
platforms.exception.ts
根目录 / project / aitoearn-backend / apps / aitoearn-server / src / core / channels / platforms / platforms.exception.ts
1 import type { AccountType, ResponseCode } from '@yikart/common'
2 import { AppException } from '@yikart/common'
3
4 export enum PlatformErrorCategory {
5 Auth = 'auth',
6 Permission = 'permission',
7 RateLimit = 'rate_limit',
8 Quota = 'quota',
9 Validation = 'validation',
10 MediaUnavailable = 'media_unavailable',
11 MediaProcessingFailed = 'media_processing_failed',
12 NotFound = 'not_found',
13 Conflict = 'conflict',
14 PlatformUnavailable = 'platform_unavailable',
15 Timeout = 'timeout',
16 Network = 'network',
17 WebhookInvalid = 'webhook_invalid',
18 SdkError = 'sdk_error',
19 Unknown = 'unknown',
20 }
21
22 export enum PlatformErrorCauseType {
23 Http = 'http',
24 Network = 'network',
25 Platform = 'platform',
26 Validation = 'validation',
27 SdkError = 'sdk_error',
28 Unknown = 'unknown',
29 }
30
31 export interface ChannelPlatformErrorContext {
32 accountId?: string
33 taskId?: string
34 platformWorkId?: string
35 endpoint?: string
36 method?: string
37 metadata?: Record<string, unknown>
38 }
39
40 export interface ChannelPlatformErrorCause {
41 type: PlatformErrorCauseType
42 httpStatus?: number
43 platformCode?: string | number
44 platformMessage?: string
45 raw?: unknown
46 quota?: {
47 usage?: number
48 total?: number
49 durationSeconds?: number
50 fbtraceId?: string
51 }
52 }
53
54 export interface ChannelPlatformExceptionInput {
55 code: ResponseCode
56 platform: AccountType
57 category: PlatformErrorCategory
58 context?: ChannelPlatformErrorContext
59 cause?: ChannelPlatformErrorCause
60 retryable?: boolean
61 }
62
63 export type PlatformSpecificExceptionInput = Omit<ChannelPlatformExceptionInput, 'platform'>
64
65 export interface PlatformValidationExceptionInput {
66 code: ResponseCode
67 category: PlatformErrorCategory
68 context?: ChannelPlatformErrorContext
69 cause?: Omit<ChannelPlatformErrorCause, 'type'>
70 }
71
72 export class ChannelPlatformException extends AppException {
73 readonly platform: AccountType
74 readonly category: PlatformErrorCategory
75 readonly context?: ChannelPlatformErrorContext
76 readonly platformCause?: ChannelPlatformErrorCause
77 readonly retryable: boolean
78
79 constructor(input: ChannelPlatformExceptionInput) {
80 super(input.code, ChannelPlatformException.toPublicData(input))
81 this.name = new.target.name
82 this.platform = input.platform
83 this.category = input.category
84 this.context = input.context
85 this.platformCause = input.cause
86 this.retryable = input.retryable ?? false
87 }
88
89 static toPublicData(input: ChannelPlatformExceptionInput): Record<string, unknown> {
90 return {
91 platform: input.platform,
92 category: input.category,
93 retryable: input.retryable ?? false,
94 accountId: input.context?.accountId,
95 taskId: input.context?.taskId,
96 platformWorkId: input.context?.platformWorkId,
97 endpoint: input.context?.endpoint,
98 httpStatus: input.cause?.httpStatus,
99 platformCode: input.cause?.platformCode,
100 }
101 }
102
103 toTaskFailure(): {
104 category: PlatformErrorCategory
105 code: string
106 message: string | undefined
107 retryable: boolean
108 originalData?: unknown
109 } {
110 return {
111 category: this.category,
112 code: String(this.code),
113 message: undefined,
114 retryable: this.retryable,
115 originalData: {
116 platformCode: this.platformCause?.platformCode,
117 httpStatus: this.platformCause?.httpStatus,
118 platformMessage: this.platformCause?.platformMessage,
119 endpoint: this.context?.endpoint,
120 method: this.context?.method,
121 raw: this.platformCause?.raw,
122 quota: this.platformCause?.quota,
123 },
124 }
125 }
126 }
127
127 lines TYPESCRIPT