@lark-apaas/coding-template-nestjs-react-fullstack 0.1.71-beta.0 → 0.1.72-alpha.20261119161335
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
import { HttpStatus, Logger } from '@nestjs/common';
|
|
2
|
+
import * as lark from '@larksuiteoapi/node-sdk';
|
|
3
|
+
import { Buffer } from 'node:buffer';
|
|
4
|
+
import { createWriteStream } from 'node:fs';
|
|
5
|
+
import { Readable } from 'node:stream';
|
|
6
|
+
import { pipeline } from 'node:stream/promises';
|
|
7
|
+
import { ResponseCode } from './constants/api_response_code';
|
|
8
|
+
import { BusinessException } from './interfaces/exception.interface';
|
|
9
|
+
|
|
10
|
+
export interface LarkCallContext {
|
|
11
|
+
/** Use the actual SDK operation, e.g. minutes.v1.minuteTranscript.get. */
|
|
12
|
+
operation: string;
|
|
13
|
+
/** Original document/Wiki/Minutes URL, not a temporary download URL. */
|
|
14
|
+
resourceUrl?: string;
|
|
15
|
+
/** Necessary scopes for this operation, when already known. */
|
|
16
|
+
requiredScopes?: readonly string[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface LarkJsonResult {
|
|
20
|
+
code?: number;
|
|
21
|
+
msg?: string;
|
|
22
|
+
request_id?: string;
|
|
23
|
+
error?: { log_id?: string; permission_violations?: unknown };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface LarkFileResult {
|
|
27
|
+
headers: unknown;
|
|
28
|
+
writeFile(filePath: string): Promise<unknown>;
|
|
29
|
+
getReadableStream(): Readable;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type LarkFailureKind =
|
|
33
|
+
| 'scope_missing'
|
|
34
|
+
| 'resource_permission'
|
|
35
|
+
| 'not_found'
|
|
36
|
+
| 'deleted'
|
|
37
|
+
| 'not_ready'
|
|
38
|
+
| 'rate_limited'
|
|
39
|
+
| 'configuration'
|
|
40
|
+
| 'invalid_request'
|
|
41
|
+
| 'upstream'
|
|
42
|
+
| 'file_processing';
|
|
43
|
+
|
|
44
|
+
interface FailureDetails {
|
|
45
|
+
provider: 'lark_openapi';
|
|
46
|
+
operation: string;
|
|
47
|
+
kind: LarkFailureKind;
|
|
48
|
+
larkCode?: number;
|
|
49
|
+
larkHttpStatus?: number;
|
|
50
|
+
requestId?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Preserves the template's error.code / error.message / error.details contract. */
|
|
54
|
+
export class LarkOpenApiException extends BusinessException {
|
|
55
|
+
public readonly kind: LarkFailureKind;
|
|
56
|
+
public readonly larkCode?: number;
|
|
57
|
+
public readonly requestId?: string;
|
|
58
|
+
|
|
59
|
+
constructor(
|
|
60
|
+
code: ResponseCode,
|
|
61
|
+
message: string,
|
|
62
|
+
httpStatus: HttpStatus,
|
|
63
|
+
details: FailureDetails,
|
|
64
|
+
) {
|
|
65
|
+
super(code, message, httpStatus, JSON.stringify(details));
|
|
66
|
+
this.name = 'LarkOpenApiException';
|
|
67
|
+
this.kind = details.kind;
|
|
68
|
+
this.larkCode = details.larkCode;
|
|
69
|
+
this.requestId = details.requestId;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let cachedClient:
|
|
74
|
+
| {
|
|
75
|
+
appId: string;
|
|
76
|
+
appSecret: string;
|
|
77
|
+
domain: lark.Domain | string;
|
|
78
|
+
client: lark.Client;
|
|
79
|
+
}
|
|
80
|
+
| undefined;
|
|
81
|
+
|
|
82
|
+
const logger = new Logger('LarkOpenAPI');
|
|
83
|
+
|
|
84
|
+
function record(value: unknown): Record<string, unknown> | undefined {
|
|
85
|
+
return typeof value === 'object' && value !== null
|
|
86
|
+
? (value as Record<string, unknown>)
|
|
87
|
+
: undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function configurationError(message: string): LarkOpenApiException {
|
|
91
|
+
return new LarkOpenApiException(
|
|
92
|
+
ResponseCode.SERVICE_UNAVAILABLE,
|
|
93
|
+
message,
|
|
94
|
+
HttpStatus.SERVICE_UNAVAILABLE,
|
|
95
|
+
{
|
|
96
|
+
provider: 'lark_openapi',
|
|
97
|
+
operation: 'client.initialize',
|
|
98
|
+
kind: 'configuration',
|
|
99
|
+
},
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function getLarkSdkDomain(): lark.Domain | string {
|
|
104
|
+
const environment =
|
|
105
|
+
process.env.FORCE_FRAMEWORK_ENVIRONMENT?.trim().toLowerCase();
|
|
106
|
+
if (environment === 'boe') return 'https://open.feishu-boe.cn';
|
|
107
|
+
if (environment === 'pre' || environment === 'online')
|
|
108
|
+
return lark.Domain.Feishu;
|
|
109
|
+
throw configurationError(
|
|
110
|
+
'飞书集成部署环境尚未就绪,请应用开发者核对平台环境配置。',
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Call inside a business method. Importing this file never initializes credentials. */
|
|
115
|
+
export function getLarkClient(): lark.Client {
|
|
116
|
+
const domain = getLarkSdkDomain();
|
|
117
|
+
const appId = process.env.LARK_APP_ID;
|
|
118
|
+
const appSecret = process.env.LARK_APP_SECRET;
|
|
119
|
+
if (!appId || !appSecret) {
|
|
120
|
+
throw configurationError(
|
|
121
|
+
'飞书集成配置尚未就绪,请核对应用绑定与环境同步。',
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
if (
|
|
125
|
+
cachedClient?.appId === appId &&
|
|
126
|
+
cachedClient.appSecret === appSecret &&
|
|
127
|
+
cachedClient.domain === domain
|
|
128
|
+
) {
|
|
129
|
+
return cachedClient.client;
|
|
130
|
+
}
|
|
131
|
+
const client = new lark.Client({
|
|
132
|
+
appId,
|
|
133
|
+
appSecret,
|
|
134
|
+
appType: lark.AppType.SelfBuild,
|
|
135
|
+
domain,
|
|
136
|
+
// SDK error formatting can include request configuration. Emit safe diagnostics below.
|
|
137
|
+
logger: { trace() {}, debug() {}, info() {}, warn() {}, error() {} },
|
|
138
|
+
});
|
|
139
|
+
cachedClient = { appId, appSecret, domain, client };
|
|
140
|
+
return client;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function header(headers: unknown, key: string): string | undefined {
|
|
144
|
+
const value = record(headers)?.[key];
|
|
145
|
+
return typeof value === 'string' && value.length <= 200 ? value : undefined;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function nativeCode(body: unknown): number | undefined {
|
|
149
|
+
const value = record(body)?.code;
|
|
150
|
+
if (typeof value === 'number' && Number.isSafeInteger(value)) return value;
|
|
151
|
+
if (typeof value === 'string' && /^\d{1,10}$/.test(value))
|
|
152
|
+
return Number(value);
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function parseJson(text: string): unknown {
|
|
157
|
+
try {
|
|
158
|
+
return JSON.parse(text);
|
|
159
|
+
} catch {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Limits apply only to error bodies; successful transcript streams are not truncated. */
|
|
165
|
+
async function errorBody(value: unknown): Promise<unknown> {
|
|
166
|
+
const limit = 64 * 1024;
|
|
167
|
+
if (typeof value === 'string') {
|
|
168
|
+
return Buffer.byteLength(value) <= limit ? parseJson(value) : undefined;
|
|
169
|
+
}
|
|
170
|
+
if (Buffer.isBuffer(value)) {
|
|
171
|
+
return value.length <= limit
|
|
172
|
+
? parseJson(value.toString('utf8'))
|
|
173
|
+
: undefined;
|
|
174
|
+
}
|
|
175
|
+
if (!(value instanceof Readable)) return record(value);
|
|
176
|
+
return new Promise((resolve) => {
|
|
177
|
+
const chunks: Buffer[] = [];
|
|
178
|
+
let bytes = 0;
|
|
179
|
+
let finished = false;
|
|
180
|
+
const finish = (body: unknown, destroy = false): void => {
|
|
181
|
+
if (finished) return;
|
|
182
|
+
finished = true;
|
|
183
|
+
clearTimeout(timer);
|
|
184
|
+
value.removeListener('data', onData);
|
|
185
|
+
value.removeListener('end', onEnd);
|
|
186
|
+
value.removeListener('error', onError);
|
|
187
|
+
value.removeListener('close', onClose);
|
|
188
|
+
if (destroy) value.destroy();
|
|
189
|
+
resolve(body);
|
|
190
|
+
};
|
|
191
|
+
const onData = (chunk: unknown): void => {
|
|
192
|
+
if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string')
|
|
193
|
+
return finish(undefined, true);
|
|
194
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
195
|
+
bytes += buffer.length;
|
|
196
|
+
if (bytes > limit) return finish(undefined, true);
|
|
197
|
+
chunks.push(buffer);
|
|
198
|
+
};
|
|
199
|
+
const onEnd = (): void =>
|
|
200
|
+
finish(parseJson(Buffer.concat(chunks).toString('utf8')));
|
|
201
|
+
const onError = (): void => finish(undefined, true);
|
|
202
|
+
const onClose = (): void => finish(undefined);
|
|
203
|
+
const timer = setTimeout(() => finish(undefined, true), 5000);
|
|
204
|
+
value.on('data', onData);
|
|
205
|
+
value.once('end', onEnd);
|
|
206
|
+
value.once('error', onError);
|
|
207
|
+
value.once('close', onClose);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function resourceLink(context: LarkCallContext): string {
|
|
212
|
+
if (!context.resourceUrl) return '';
|
|
213
|
+
try {
|
|
214
|
+
const url = new URL(context.resourceUrl);
|
|
215
|
+
if (
|
|
216
|
+
!['https:', 'http:'].includes(url.protocol) ||
|
|
217
|
+
url.username ||
|
|
218
|
+
url.password
|
|
219
|
+
)
|
|
220
|
+
return '';
|
|
221
|
+
const href = url.href.replace(/[()<>]/g, (character: string) =>
|
|
222
|
+
encodeURIComponent(character),
|
|
223
|
+
);
|
|
224
|
+
const label = context.operation.startsWith('minutes.')
|
|
225
|
+
? '打开妙记'
|
|
226
|
+
: context.operation.startsWith('wiki.')
|
|
227
|
+
? '打开知识库节点'
|
|
228
|
+
: '打开文档';
|
|
229
|
+
return ` [${label}](${href})`;
|
|
230
|
+
} catch {
|
|
231
|
+
return '';
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function scopeLink(body: unknown, context: LarkCallContext): string {
|
|
236
|
+
const violations = record(record(body)?.error)?.permission_violations;
|
|
237
|
+
if (!Array.isArray(violations)) return '';
|
|
238
|
+
const scopes = [
|
|
239
|
+
...new Set(
|
|
240
|
+
violations.flatMap((item: unknown): string[] => {
|
|
241
|
+
const violation = record(item);
|
|
242
|
+
return violation?.type === 'action_scope_required' &&
|
|
243
|
+
typeof violation.subject === 'string' &&
|
|
244
|
+
/^[a-zA-Z0-9_:.-]+$/.test(violation.subject)
|
|
245
|
+
? [violation.subject]
|
|
246
|
+
: [];
|
|
247
|
+
}),
|
|
248
|
+
),
|
|
249
|
+
];
|
|
250
|
+
if (!scopes.length) return '';
|
|
251
|
+
const message = record(body)?.msg;
|
|
252
|
+
const anyOf = typeof message === 'string' && /one of|任一/i.test(message);
|
|
253
|
+
const known = context.requiredScopes?.filter((scope: string) =>
|
|
254
|
+
scopes.includes(scope),
|
|
255
|
+
);
|
|
256
|
+
const preferred =
|
|
257
|
+
context.operation === 'minutes.v1.minuteTranscript.get'
|
|
258
|
+
? 'minutes:minutes.transcript:export'
|
|
259
|
+
: context.operation === 'minutes.v1.minute.get'
|
|
260
|
+
? 'minutes:minutes.basic:read'
|
|
261
|
+
: context.operation.startsWith('docx.')
|
|
262
|
+
? 'docx:document:readonly'
|
|
263
|
+
: context.operation === 'wiki.v2.space.getNode'
|
|
264
|
+
? 'wiki:wiki:readonly'
|
|
265
|
+
: undefined;
|
|
266
|
+
const selected = anyOf
|
|
267
|
+
? [
|
|
268
|
+
(preferred && scopes.includes(preferred) ? preferred : undefined) ??
|
|
269
|
+
known?.[0] ??
|
|
270
|
+
scopes.find((scope: string) =>
|
|
271
|
+
/:readonly$|\.basic:read$/.test(scope),
|
|
272
|
+
) ??
|
|
273
|
+
scopes[0],
|
|
274
|
+
]
|
|
275
|
+
: scopes;
|
|
276
|
+
const appId = process.env.LARK_APP_ID;
|
|
277
|
+
if (!appId || !/^cli_[a-zA-Z0-9]+$/.test(appId)) return '';
|
|
278
|
+
const domain = getLarkSdkDomain();
|
|
279
|
+
const base = typeof domain === 'string' ? domain : 'https://open.feishu.cn';
|
|
280
|
+
return ` 请[申请飞书应用权限](${base}/app/${appId}/auth?q=${encodeURIComponent(selected.join(','))}&op_from=openapi),权限审批及发布生效后重试。`;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function failure(
|
|
284
|
+
context: LarkCallContext,
|
|
285
|
+
body?: unknown,
|
|
286
|
+
httpStatus?: number,
|
|
287
|
+
headers?: unknown,
|
|
288
|
+
kindOverride?: LarkFailureKind,
|
|
289
|
+
): LarkOpenApiException {
|
|
290
|
+
const code = nativeCode(body);
|
|
291
|
+
const error = record(record(body)?.error);
|
|
292
|
+
const id =
|
|
293
|
+
record(body)?.request_id ?? error?.log_id ?? header(headers, 'x-tt-logid');
|
|
294
|
+
const requestId = typeof id === 'string' && id.length <= 200 ? id : undefined;
|
|
295
|
+
let kind: LarkFailureKind = kindOverride ?? 'upstream';
|
|
296
|
+
let responseCode = ResponseCode.BAD_GATEWAY;
|
|
297
|
+
let status = HttpStatus.BAD_GATEWAY;
|
|
298
|
+
let message = '飞书调用失败,请应用开发者根据诊断信息排查。';
|
|
299
|
+
if (kindOverride === 'file_processing') {
|
|
300
|
+
responseCode = ResponseCode.INTERNAL_ERROR;
|
|
301
|
+
status = HttpStatus.INTERNAL_SERVER_ERROR;
|
|
302
|
+
message = '飞书文件读取或保存失败,请应用开发者排查文件处理过程。';
|
|
303
|
+
} else if (code === 99991672) {
|
|
304
|
+
kind = 'scope_missing';
|
|
305
|
+
responseCode = ResponseCode.FORBIDDEN;
|
|
306
|
+
status = HttpStatus.FORBIDDEN;
|
|
307
|
+
message =
|
|
308
|
+
'当前绑定飞书应用缺少本次调用所需的 API 权限。请该应用管理员处理权限配置。' +
|
|
309
|
+
scopeLink(body, context);
|
|
310
|
+
} else if (code === 1770032 || code === 131006 || code === 2091005) {
|
|
311
|
+
kind = 'resource_permission';
|
|
312
|
+
responseCode = ResponseCode.FORBIDDEN;
|
|
313
|
+
status = HttpStatus.FORBIDDEN;
|
|
314
|
+
if (code === 1770032) {
|
|
315
|
+
message =
|
|
316
|
+
'当前绑定飞书应用没有这份文档的阅读权限。请文档拥有者或管理者通过右上角“…”→“…更多”→“添加文档应用”为该应用授权。';
|
|
317
|
+
} else if (code === 131006) {
|
|
318
|
+
message =
|
|
319
|
+
'当前绑定飞书应用没有该知识库节点或空间的访问权限。请知识库管理员或节点管理者为该应用授权所需节点范围。';
|
|
320
|
+
} else if (context.operation === 'minutes.v1.minuteTranscript.get') {
|
|
321
|
+
message =
|
|
322
|
+
'当前绑定飞书应用无法导出这篇妙记的文字记录。请妙记拥有者或管理者确认资源访问范围与导出权限。';
|
|
323
|
+
} else if (context.operation === 'minutes.v1.minute.get') {
|
|
324
|
+
message =
|
|
325
|
+
'当前绑定飞书应用无法读取这篇妙记的基本信息。请妙记拥有者或管理者确认资源阅读权限。';
|
|
326
|
+
} else {
|
|
327
|
+
message =
|
|
328
|
+
'当前绑定飞书应用无权执行本次妙记操作。请妙记拥有者或管理者确认资源与操作权限。';
|
|
329
|
+
}
|
|
330
|
+
message += resourceLink(context);
|
|
331
|
+
} else if ([1770002, 131005, 2091002].includes(code ?? -1)) {
|
|
332
|
+
kind = 'not_found';
|
|
333
|
+
responseCode = ResponseCode.NOT_FOUND;
|
|
334
|
+
status = HttpStatus.NOT_FOUND;
|
|
335
|
+
message =
|
|
336
|
+
'无法找到指定飞书资源,请检查原始链接、资源标识及实际访问范围。' +
|
|
337
|
+
resourceLink(context);
|
|
338
|
+
} else if (code === 1770003 || code === 2091004) {
|
|
339
|
+
kind = 'deleted';
|
|
340
|
+
responseCode = ResponseCode.NOT_FOUND;
|
|
341
|
+
status = HttpStatus.NOT_FOUND;
|
|
342
|
+
message = '指定飞书资源已被删除,请确认原始资源。' + resourceLink(context);
|
|
343
|
+
} else if (code === 2091003) {
|
|
344
|
+
kind = 'not_ready';
|
|
345
|
+
responseCode = ResponseCode.CONFLICT;
|
|
346
|
+
status = HttpStatus.CONFLICT;
|
|
347
|
+
message =
|
|
348
|
+
'妙记文字记录尚未就绪,请等待转写完成后重试。' + resourceLink(context);
|
|
349
|
+
} else if (code === 99991400 || httpStatus === 429) {
|
|
350
|
+
kind = 'rate_limited';
|
|
351
|
+
responseCode = ResponseCode.TOO_MANY_REQUESTS;
|
|
352
|
+
status = HttpStatus.TOO_MANY_REQUESTS;
|
|
353
|
+
message = '飞书请求达到频率限制,请稍后重试。';
|
|
354
|
+
} else if (code === 10014 || code === 99991671) {
|
|
355
|
+
kind = 'configuration';
|
|
356
|
+
responseCode = ResponseCode.SERVICE_UNAVAILABLE;
|
|
357
|
+
status = HttpStatus.SERVICE_UNAVAILABLE;
|
|
358
|
+
message = '飞书调用身份配置未生效,请应用开发者核对绑定与平台环境。';
|
|
359
|
+
} else if (code === 2091001 || httpStatus === 400) {
|
|
360
|
+
kind = 'invalid_request';
|
|
361
|
+
responseCode = ResponseCode.BAD_REQUEST;
|
|
362
|
+
status = HttpStatus.BAD_REQUEST;
|
|
363
|
+
message = '飞书请求参数有误,请应用开发者核对资源标识与本次操作的参数。';
|
|
364
|
+
}
|
|
365
|
+
const details: FailureDetails = {
|
|
366
|
+
provider: 'lark_openapi',
|
|
367
|
+
operation: context.operation,
|
|
368
|
+
kind,
|
|
369
|
+
larkCode: code,
|
|
370
|
+
larkHttpStatus: httpStatus,
|
|
371
|
+
requestId,
|
|
372
|
+
};
|
|
373
|
+
// Never serialize the original SDK error, credentials, request config or transcript.
|
|
374
|
+
logger.warn(details);
|
|
375
|
+
return new LarkOpenApiException(responseCode, message, status, details);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
async function requestFailure(
|
|
379
|
+
error: unknown,
|
|
380
|
+
context: LarkCallContext,
|
|
381
|
+
): Promise<LarkOpenApiException> {
|
|
382
|
+
const response = record(record(error)?.response);
|
|
383
|
+
const body = await errorBody(response?.data ?? error);
|
|
384
|
+
const status =
|
|
385
|
+
typeof response?.status === 'number' ? response.status : undefined;
|
|
386
|
+
return failure(context, body, status, response?.headers);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** Checks the JSON business code as well as rejected HTTP requests; never retries writes. */
|
|
390
|
+
export async function callLarkJson<T extends LarkJsonResult>(
|
|
391
|
+
context: LarkCallContext,
|
|
392
|
+
request: (client: lark.Client) => Promise<T>,
|
|
393
|
+
): Promise<T> {
|
|
394
|
+
try {
|
|
395
|
+
const result = await request(getLarkClient());
|
|
396
|
+
if (result?.code !== 0) throw failure(context, result);
|
|
397
|
+
return result;
|
|
398
|
+
} catch (error: unknown) {
|
|
399
|
+
if (error instanceof BusinessException) throw error;
|
|
400
|
+
throw await requestFailure(error, context);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Consume the successful download once; local consumption errors are not resource permissions. */
|
|
405
|
+
export async function callLarkFile<T>(
|
|
406
|
+
context: LarkCallContext,
|
|
407
|
+
request: (client: lark.Client) => Promise<LarkFileResult>,
|
|
408
|
+
consume: (file: LarkFileResult) => Promise<T>,
|
|
409
|
+
): Promise<T> {
|
|
410
|
+
let file: LarkFileResult;
|
|
411
|
+
try {
|
|
412
|
+
file = await request(getLarkClient());
|
|
413
|
+
if (
|
|
414
|
+
typeof file?.getReadableStream !== 'function' ||
|
|
415
|
+
typeof file.writeFile !== 'function'
|
|
416
|
+
) {
|
|
417
|
+
throw failure(context, file);
|
|
418
|
+
}
|
|
419
|
+
if (header(file.headers, 'content-type')?.includes('application/json')) {
|
|
420
|
+
throw failure(
|
|
421
|
+
context,
|
|
422
|
+
await errorBody(file.getReadableStream()),
|
|
423
|
+
200,
|
|
424
|
+
file.headers,
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
} catch (error: unknown) {
|
|
428
|
+
if (error instanceof BusinessException) throw error;
|
|
429
|
+
throw await requestFailure(error, context);
|
|
430
|
+
}
|
|
431
|
+
// Application validation/database errors belong to the caller and retain their contract.
|
|
432
|
+
return consume({
|
|
433
|
+
headers: file.headers,
|
|
434
|
+
getReadableStream: () => file.getReadableStream(),
|
|
435
|
+
// Only this file I/O boundary converts source-stream and destination-file failures.
|
|
436
|
+
writeFile: async (filePath: string) => {
|
|
437
|
+
try {
|
|
438
|
+
await pipeline(file.getReadableStream(), createWriteStream(filePath));
|
|
439
|
+
return filePath;
|
|
440
|
+
} catch (error: unknown) {
|
|
441
|
+
if (error instanceof BusinessException) throw error;
|
|
442
|
+
throw failure(
|
|
443
|
+
context,
|
|
444
|
+
undefined,
|
|
445
|
+
undefined,
|
|
446
|
+
file.headers,
|
|
447
|
+
'file_processing',
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
},
|
|
451
|
+
});
|
|
452
|
+
}
|