Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix#437ファイルアップロードのエラーがキャッチされない #438

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/common_components/form_editor/FilesEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ async function uploadFiles(files: FileList): Promise<filesStatus[]> {
const res = await toast.promise(postFiles("public", new Map<string, FileList>([["attachments", files]])), {
loading: "ファイルをアップロードしています",
success: "ファイルのアップロードに成功しました",
error: "ファイルのアップロードに失敗しました",
error: (error) => {
return "ファイルのアップロードに失敗しました" + (error?.message ? ` (${error?.message})` : "");
},
});
return (
res?.attachments.map((uuid: string, index: number) => ({
Expand Down
33 changes: 24 additions & 9 deletions src/lib/postFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,39 @@ export const postFiles = async (visibility: Visibilities, files: FilesFormType)
if (!f) {
return;
}
try {
const response = (await postFile(visibility, f))?.ids;
return response;
} catch {
return false;
}
return await new Promise(async (resolve, reject) => {
try {
const response = (await postFile(visibility, f))?.ids;
resolve(response);
} catch (e: unknown) {
reject(e);
}
})
.then((response) => {
return response;
})
.catch(() => {
return false;
});
}),
)
).flat();

if (ids.some((v) => !v)) {
const safeIds: (string | false | undefined)[] = ids.map((id) => {
if (typeof id === "string") return id;
if (id === false) return false;
if (id === undefined) return undefined;
return undefined;
});

if (safeIds.some((v) => !v)) {
// エラーがあったら全てのファイルを削除した上でreturn
await deleteMultipleUploadedFiles(ids);
await deleteMultipleUploadedFiles(safeIds);
return;
}

// falsyのもの以外を抽出(本来上の条件分岐で絞り込めているはずだが、現在のTSのバージョン(5.3.3)ではうまく推論されないためこれを書いている)
fileIds[file[0]] = ids.flatMap((v) => v || []).concat(alreadyUploaded);
fileIds[file[0]] = safeIds.flatMap((v) => v || []).concat(alreadyUploaded);
}
return fileIds;
};
Expand Down
Loading