fix: isolate attachment submission state
This commit is contained in:
@@ -66,28 +66,32 @@ async function readBoundedBody(req: IncomingMessage): Promise<Uint8Array> {
|
||||
return Buffer.concat(chunks);
|
||||
}
|
||||
|
||||
function sendAttachmentError(res: ServerResponse, error: unknown): void {
|
||||
const status = typeof (error as { status?: unknown })?.status === 'number'
|
||||
function sendAttachmentError(
|
||||
res: ServerResponse,
|
||||
error: unknown,
|
||||
operation: 'upload' | 'content',
|
||||
): void {
|
||||
const explicitStatus = typeof (error as { status?: unknown })?.status === 'number'
|
||||
? (error as { status: number }).status
|
||||
: (error as NodeJS.ErrnoException)?.code === 'ENOENT'
|
||||
? 404
|
||||
: 500;
|
||||
: null;
|
||||
const status = explicitStatus
|
||||
?? (operation === 'content' || (error as NodeJS.ErrnoException)?.code === 'ENOENT' ? 404 : 500);
|
||||
const message = status === 413
|
||||
? '图片不能超过 16 MB。'
|
||||
: status === 404
|
||||
? '图片附件不存在。'
|
||||
: status === 400
|
||||
? '图片附件无效。'
|
||||
: '图片附件暂时无法读取。';
|
||||
: operation === 'upload'
|
||||
? '本地数据写入失败,请检查存储后重试。'
|
||||
: '图片附件暂时无法读取。';
|
||||
sendJson(res, status, {
|
||||
success: false,
|
||||
code: status === 413
|
||||
? 'CODING_ATTACHMENT_TOO_LARGE'
|
||||
code: status === 400 || status === 413
|
||||
? 'CODING_ATTACHMENT_INVALID'
|
||||
: status === 404
|
||||
? 'CODING_ATTACHMENT_NOT_FOUND'
|
||||
: status === 400
|
||||
? 'CODING_ATTACHMENT_INVALID'
|
||||
: 'CODING_ATTACHMENT_STORAGE_FAILED',
|
||||
: 'CODING_STORAGE_WRITE_FAILED',
|
||||
error: message,
|
||||
});
|
||||
}
|
||||
@@ -147,7 +151,7 @@ export async function handleCodingAttachmentRoutes(
|
||||
res.end(record.data);
|
||||
return true;
|
||||
} catch (error) {
|
||||
sendAttachmentError(res, error);
|
||||
sendAttachmentError(res, error, upload ? 'upload' : 'content');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user