diff --git a/src/remote-ajax.js b/src/remote-ajax.js index f270605..82fcd97 100644 --- a/src/remote-ajax.js +++ b/src/remote-ajax.js @@ -65,6 +65,10 @@ module.exports.downloadFileOrUrl = async function(pathOrUrl, target) { redirect: 'follow' }); + if (!response.ok) { + throw new Error(`HTTP request returned error: ${response.status}: ${response.statusText}`); + } + let fd = await fs.open(target, 'w'); let length = 0; try { @@ -89,9 +93,5 @@ module.exports.downloadFileOrUrl = async function(pathOrUrl, target) { await fs.close(fd); } - if (!response.ok) { - throw new Error(`HTTP request returned error: ${response.status}: ${response.statusText}`); - } - return length; };