Skip to content

Commit

Permalink
Merge pull request sass#909 from saper/fix/httperr
Browse files Browse the repository at this point in the history
Improve binary file download error handling
  • Loading branch information
xzyfer committed May 5, 2015
2 parents 1bf73b5 + 988d4e2 commit d266478
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,39 @@ require('../lib/extensions');
*/

function download(url, dest, cb) {
var returnError = function(err) {
cb(typeof err.message === 'string' ? err.message : err);
var reportError = function(err) {
cb(['Cannot download "', url, '": ',
typeof err.message === 'string' ? err.message : err].join(''));
};
var successful = function(response) {
return response.statusCode >= 200 && response.statusCode < 300;
};

if (url) {
applyProxy({ rejectUnauthorized: false }, function(options) {
options.headers = {
'User-Agent': [
'node/', process.version, ' ',
'node-sass-installer/', package.version
].join('')
};
request.get(url, options).on('response', function(response) {
if (response.statusCode < 200 || response.statusCode >= 300) {
returnError(['Can not download file from:', url].join());
return;
applyProxy({ rejectUnauthorized: false }, function(options) {
options.headers = {
'User-Agent': [
'node/', process.version, ' ',
'node-sass-installer/', package.version
].join('')
};
try {
request(url, options, function(err, response) {
if (err) {
reportError(err);
} else if (!successful(response)) {
reportError(['HTTP error', response.statusCode, response.statusMessage].join(' '));
} else {
cb();
}

response.pipe(fs.createWriteStream(dest));

cb();
}).on('error', returnError);
});
} else {
returnError('Download URL not defined, set SASS_BINARY_SITE in the environment to enable download.');
}
}).on('response', function(response) {
if (successful(response)) {
response.pipe(fs.createWriteStream(dest));
}
});
} catch (err) {
cb(err);
}
});
}

/**
Expand Down

0 comments on commit d266478

Please sign in to comment.