Skip to content

Commit

Permalink
Improve error handling in downloadFile function
Browse files Browse the repository at this point in the history
  • Loading branch information
niklashigi committed Nov 29, 2020
1 parent 0288dfc commit ab95829
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/utils/download-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ function downloadFile(
const downloadFilePath = finalFilePath + '.dl'

https.get(url, response => {
if (response.statusCode !== 200) {
const error = new Error(`The URL "${url}" returned status code ${response.statusCode}, expected 200.`)

// Cancel download with error
response.destroy(error)
}

const fileStream = fs.createWriteStream(downloadFilePath)

const totalLength = parseInt(response.headers['content-length'])
Expand All @@ -67,7 +74,7 @@ function downloadFile(
await fsp.rename(downloadFilePath, finalFilePath)
subscriber.complete()
})
}).on('error', subscriber.error)
}).on('error', error => subscriber.error(error))
})()
})
}
Expand Down

0 comments on commit ab95829

Please sign in to comment.