From 29516d2b1a48af76f6253b145319c3b66a1237df Mon Sep 17 00:00:00 2001 From: Hari Juturu Date: Sun, 25 Mar 2018 19:32:56 -0700 Subject: [PATCH] Don't save the response if HTTP response is not OK --- src/remote-ajax.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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; };