Skip to content

Commit

Permalink
Merge pull request #438 from sohosai/fix-437
Browse files Browse the repository at this point in the history
fix#437ファイルアップロードのエラーがキャッチされない
  • Loading branch information
appare45 authored Aug 27, 2024
2 parents b84d81e + 81b6ca6 commit 1d54738
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
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

0 comments on commit 1d54738

Please sign in to comment.