Skip to content

Commit 7eadc4c

Browse files
committed
fix: Handle download error if file does not exist
1 parent b5d4f93 commit 7eadc4c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

packages/file-transfer/download.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,6 @@ export async function download(options: DownloadOptions) {
281281
const dispatcher = options?.dispatcher ?? getDefaultAgent()
282282
const expectedTotal = options.expectedTotal
283283

284-
await mkdir(dirname(destination), { recursive: true }).catch(() => { })
285-
286284
if (!skipPrevalidate && validator) {
287285
const error = await validator.validate(destination, urls[0]).catch((e) => e)
288286
if (!error) {
@@ -306,7 +304,8 @@ export async function download(options: DownloadOptions) {
306304
}
307305

308306
const output = pendingFile || destination
309-
const fd = await open(output, 'w').catch((e) => {
307+
await mkdir(dirname(destination), { recursive: true }).catch(() => { })
308+
function assignError(e: Error) {
310309
e.stack = new Error().stack
311310
Object.assign(e, {
312311
phase: 'open',
@@ -315,6 +314,16 @@ export async function download(options: DownloadOptions) {
315314
destination,
316315
pendingFile,
317316
})
317+
}
318+
const fd = await open(output, 'w').catch(async (e) => {
319+
if (e.code === 'ENOENT') {
320+
await mkdir(dirname(destination), { recursive: true })
321+
return await open(output, 'w').catch((e) => {
322+
assignError(e)
323+
throw e
324+
})
325+
}
326+
assignError(e)
318327
throw e
319328
})
320329

0 commit comments

Comments
 (0)