From a902576334ba423b81201341b30c8dfaf544ac8a Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 19 Nov 2021 18:57:34 +0900 Subject: [PATCH] fix(client): better error handling of file upload --- packages/client/src/os.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/client/src/os.ts b/packages/client/src/os.ts index 2bd270a97..9eac06a5e 100644 --- a/packages/client/src/os.ts +++ b/packages/client/src/os.ts @@ -563,13 +563,19 @@ export function post(props: Record) { export const deckGlobalEvents = new EventEmitter(); -export const uploads = ref([]); +export const uploads = ref<{ + id: string; + name: string; + progressMax: number | undefined; + progressValue: number | undefined; + img: string; +}[]>([]); export function upload(file: File, folder?: any, name?: string) { if (folder && typeof folder == 'object') folder = folder.id; return new Promise((resolve, reject) => { - const id = Math.random(); + const id = Math.random().toString(); const reader = new FileReader(); reader.onload = (e) => { @@ -593,8 +599,21 @@ export function upload(file: File, folder?: any, name?: string) { const xhr = new XMLHttpRequest(); xhr.open('POST', apiUrl + '/drive/files/create', true); - xhr.onload = (e: any) => { - const driveFile = JSON.parse(e.target.response); + xhr.onload = (ev) => { + if (ev.target == null || ev.target.response == null) { + // TODO: 消すのではなくて再送できるようにしたい + uploads.value = uploads.value.filter(x => x.id != id); + + alert({ + type: 'error', + text: 'upload failed' + }); + + reject(); + return; + } + + const driveFile = JSON.parse(ev.target.response); resolve(driveFile);