fix: replace with empty string if error in GET

Fixes #61

It seems to work, but I'm not sure if it's a clean way of doing it. Let
me know what you think @remy.
This commit is contained in:
Sam Nguyen 2015-08-28 20:13:36 -07:00
parent 9904b6b334
commit 8af810faa7
5 changed files with 35 additions and 5 deletions

View File

@ -54,12 +54,20 @@ module.exports = function get(url, options) {
return reject(error);
}
debug('response: %s %s', res.statusCode, url);
if (!error && res.statusCode === 200) {
resolve({ headers: res.headers, body: body });
} else {
return reject(new Error(res.statusCode));
if (!res) {
res = { headers: {}, statusCode: null };
}
debug('response: %s %s', res.statusCode, url);
if (res.statusCode !== 200) {
body = '';
}
resolve({
body: body,
headers: res.headers,
statusCode: res.statusCode,
});
});
});

View File

@ -146,6 +146,10 @@ function main() {
var cheerioLoadOptions = {};
var enc = inliner.options.encoding;
if (res.statusCode !== 200) {
inliner.emit('progress', res.statusCode + ' on ' + url);
}
// try to determine the encoding from the headers and the body
if (!enc) {
enc = charset(res.headers, res.body);

View File

@ -5,6 +5,11 @@ var UglifyJS = require('uglify-js');
function uglify(source) {
this.emit('progress', 'compressing javascript');
if (source.body === '') {
return '';
}
if (source.body) {
source = source.body;
}

1
test/fixtures/get-error.result.html vendored Normal file
View File

@ -0,0 +1 @@
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <meta charset="UTF-8"> <title>App</title> <style></style> <script></script> </head> <body> </body> </html>

12
test/fixtures/get-error.src.html vendored Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta charset="UTF-8" />
<title>App</title>
<link rel="stylesheet" href="http://example.com/this_doesnt_exist" />
<script src="http://example.com/this_doesnt_exist"></script>
</head>
<body>
</body>
</html>