From 8af810faa74285d4d75ea9599d244c5ee3cf09ee Mon Sep 17 00:00:00 2001 From: Sam Nguyen Date: Fri, 28 Aug 2015 20:13:36 -0700 Subject: [PATCH] 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. --- lib/get.js | 16 ++++++++++++---- lib/index.js | 4 ++++ lib/javascript.js | 5 +++++ test/fixtures/get-error.result.html | 1 + test/fixtures/get-error.src.html | 12 ++++++++++++ 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/get-error.result.html create mode 100644 test/fixtures/get-error.src.html diff --git a/lib/get.js b/lib/get.js index 20af3ba..c768d32 100644 --- a/lib/get.js +++ b/lib/get.js @@ -54,12 +54,20 @@ module.exports = function get(url, options) { return reject(error); } + if (!res) { + res = { headers: {}, statusCode: null }; + } + 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.statusCode !== 200) { + body = ''; } + + resolve({ + body: body, + headers: res.headers, + statusCode: res.statusCode, + }); }); }); diff --git a/lib/index.js b/lib/index.js index 70cd351..85f8944 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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); diff --git a/lib/javascript.js b/lib/javascript.js index fc37a52..bd3b8b3 100644 --- a/lib/javascript.js +++ b/lib/javascript.js @@ -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; } diff --git a/test/fixtures/get-error.result.html b/test/fixtures/get-error.result.html new file mode 100644 index 0000000..29aa926 --- /dev/null +++ b/test/fixtures/get-error.result.html @@ -0,0 +1 @@ + App \ No newline at end of file diff --git a/test/fixtures/get-error.src.html b/test/fixtures/get-error.src.html new file mode 100644 index 0000000..5aff9e8 --- /dev/null +++ b/test/fixtures/get-error.src.html @@ -0,0 +1,12 @@ + + + + + + App + + + + + + \ No newline at end of file