mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
ui: preserve download errors when aborting
This commit is contained in:
1 parent
0d6aa7788c
commit
fae88b3c7a
1 file changed
+8
-7
@@ -30,14 +30,16 @@ export default async function download(filename, targets) {
|
|||||||
const fileStream = streamsaver.createWriteStream(filename);
|
const fileStream = streamsaver.createWriteStream(filename);
|
||||||
const queue = new PQueue({ concurrency: 5 });
|
const queue = new PQueue({ concurrency: 5 });
|
||||||
const abortCallbackReceiver: any = {};
|
const abortCallbackReceiver: any = {};
|
||||||
function stopDownload() { abortCallbackReceiver.abort?.(); }
|
function stopDownload(reason?: unknown) {
|
||||||
|
abortCallbackReceiver.abort?.(reason instanceof Error ? reason : new Error('Download aborted'));
|
||||||
|
}
|
||||||
let i = 0;
|
let i = 0;
|
||||||
async function downloadFile(target, retry = 5) {
|
async function downloadFile(target, retry = 5) {
|
||||||
try {
|
try {
|
||||||
let stream;
|
let stream;
|
||||||
if (target.url) {
|
if (target.url) {
|
||||||
const response = await fetch(target.url);
|
const response = await fetch(target.url);
|
||||||
if (!response.ok) throw response.statusText;
|
if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
|
||||||
stream = response.body;
|
stream = response.body;
|
||||||
} else {
|
} else {
|
||||||
stream = new Blob([target.content]).stream();
|
stream = new Blob([target.content]).stream();
|
||||||
@@ -48,15 +50,14 @@ export default async function download(filename, targets) {
|
|||||||
};
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (retry) {
|
if (retry) {
|
||||||
Notification.warn(i18n('Download Error: {0} {1}, retry in 3 secs...', [target.filename, e.toString()]));
|
Notification.warn(i18n('Download Error: {0} {1}, retry in 3 secs...', [target.filename, String(e)]));
|
||||||
await sleep(3000);
|
await sleep(3000);
|
||||||
return await downloadFile(target, retry - 1);
|
return await downloadFile(target, retry - 1);
|
||||||
}
|
}
|
||||||
window.captureException?.(e);
|
const error = new Error(i18n('Download Error: {0} {1}', [target.filename, String(e)]));
|
||||||
stopDownload();
|
stopDownload(error);
|
||||||
Notification.error(i18n('Download Error: {0} {1}', [target.filename, e.toString()]));
|
throw error;
|
||||||
}
|
}
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
const handles = [];
|
const handles = [];
|
||||||
for (const target of targets) {
|
for (const target of targets) {
|
||||||
|
|||||||
Reference in new issue
Block a user