Skip to content

Commit ae880f7

Browse files
committed
Handle HTTP error status when downloading
1 parent 1052057 commit ae880f7

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const binaryName = 'myst';
1212

1313
const getFileName = function (osPathPart, archPathPart, extension) {
1414
return binaryName + '_' + osPathPart + '_' + archPathPart + extension
15-
}
15+
};
1616

1717
const getDownloadInfo = function (version, osType, architecture) {
1818
osType = osType.toLowerCase();
@@ -37,10 +37,14 @@ const getDownloadInfo = function (version, osType, architecture) {
3737
let download = function (url, dest, cb) {
3838
let file = fs.createWriteStream(dest);
3939
https.get(url, function (response) {
40-
response.pipe(file);
4140
file.on('finish', function () {
4241
file.close(cb);
4342
});
43+
if (response.statusCode >= 400) {
44+
if (cb) cb(new Error(`Unsuccessful HTTP status: ${url} ${response.statusCode} ${response.statusMessage}`));
45+
return
46+
}
47+
response.pipe(file);
4448
}).on('error', function (err) {
4549
fs.unlink(dest);
4650
if (cb) cb(err);
@@ -63,14 +67,14 @@ module.exports = function (osType, architecture, destination) {
6367
const {url, filename} = getDownloadInfo(version, osType, architecture);
6468
download(url, filename, function (err) {
6569
if (err) return console.log(err);
66-
console.log('downloaded', url);
70+
console.log('Downloaded', url);
6771
const format = osType.includes('windows') ? 'zip' : 'tar.gz';
6872
unpack(filename, destination, format, function (err) {
6973
if (err) return console.error(err);
70-
console.log('unpacked to ', destination);
74+
console.log('Unpacked to ', destination);
7175
fs.unlink(filename, (err) => {
7276
if (err) return console.error(err);
73-
console.log('deleted', filename);
77+
console.log('Deleted', filename);
7478
})
7579
})
7680
});

0 commit comments

Comments
 (0)