Repository navigation
Expand file tree
/
Copy patherrors.ts
More file actions
117 lines (104 loc) · 3.31 KB
/
Copy patherrors.ts
File metadata and controls
117 lines (104 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* MCP/API 공통 에러 진단 구조
*/
export interface StandardErrorDiagnostics {
code: string;
message: string;
status?: number;
retryable: boolean;
service?: string;
operation?: string;
upstreamStatus?: number;
hint: string;
}
const SERVICE_PREFIXES = [
'daiso',
'gs25',
'seveneleven',
'cu',
'emart24',
'lottemart',
'megabox',
'lottecinema',
'cgv',
'dtryx',
'oliveyoung',
] as const;
function normalizeService(value: string): string {
return value.toLowerCase().replace(/_/g, '');
}
function toOperation(parts: string[]): string | undefined {
const filtered = parts.filter((part) => part.length > 0 && part !== 'FAILED');
if (filtered.length === 0) {
return undefined;
}
return filtered.map((part) => part.toLowerCase()).join('_');
}
function inferServiceAndOperation(
code: string,
): Pick<StandardErrorDiagnostics, 'service' | 'operation'> {
const parts = code.split('_').filter((part) => part.length > 0);
const normalizedFirst = normalizeService(parts[0] || '');
const service = SERVICE_PREFIXES.find((item) => normalizeService(item) === normalizedFirst);
if (!service) {
return { operation: toOperation(parts) };
}
return {
service,
operation: toOperation(parts.slice(1)),
};
}
function isRetryable(code: string, status?: number): boolean {
if (typeof status === 'number') {
return status >= 500 || status === 408 || status === 429;
}
const normalized = code.toUpperCase();
return normalized.includes('TIMEOUT') || normalized.includes('FAILED');
}
function buildHint(retryable: boolean): string {
return retryable
? '일시적인 외부 서비스 오류일 수 있습니다. 잠시 후 다시 시도하세요.'
: '입력값 또는 요청 조건을 확인하세요.';
}
export function toStandardErrorDiagnostics(
code: string,
message: string,
options: {
status?: number;
operation?: string;
service?: string;
upstreamStatus?: number;
} = {},
): StandardErrorDiagnostics {
const inferred = inferServiceAndOperation(code);
// 실제 Zyte 진단 문구만 설정 오류로 취급해 일반적인 접근 차단과 구분합니다.
const configurationError = [
'Zyte API 호출 실패: 403 Your account has been suspended.',
'Zyte API 호출 실패: 403 account suspended',
'ZYTE_API_KEY가 설정되지 않았습니다.',
].some((diagnostic) => message.includes(diagnostic));
const gs25AuthenticationError =
code === 'GS25_UPSTREAM_UNAVAILABLE' &&
message.includes('GS25 재고 서비스 인증을 사용할 수 없습니다.');
const retryable =
!configurationError &&
!gs25AuthenticationError &&
isRetryable(code, options.status || options.upstreamStatus);
return {
code,
message,
status: options.status,
retryable,
service: options.service || inferred.service,
operation: options.operation || inferred.operation,
upstreamStatus: options.upstreamStatus,
hint: gs25AuthenticationError
? '운영자는 GS25_API_KEY 설정을 확인하세요.'
: configurationError
? '운영자는 ZYTE_API_KEY 설정과 Zyte 계정 상태를 확인하세요.'
: buildHint(retryable),
};
}
export function getErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.';
}