Skip to content

Commit

Permalink
Catch fatal xml/html parsing errors and pass them to callback
Browse files Browse the repository at this point in the history
Xml parsing is more strict and have more unrecoverable errors like
absence of root element or multiply root elements.
  • Loading branch information
samogot committed May 12, 2018
1 parent d45f4fc commit 9779590
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ function Request(method, url, params, opts, tries, callback) {
var parse_mode = opts.parse_as;
if (parse_mode === 'auto')
parse_mode = getResponseType(res.headers['content-type']);
if (parse_mode === 'xml')
document = libxml.parseXml(document, { baseUrl: location.href, huge: true });
else
document = libxml.parseHtml(document, { baseUrl: location.href, huge: true });
try {
if (parse_mode === 'xml')
document = libxml.parseXml(document, {baseUrl: location.href, huge: true});
else
document = libxml.parseHtml(document, {baseUrl: location.href, huge: true});
} catch (e) {
callback('Response ' + parse_mode + ' parsing error: ' + e);
return;
}

if (document === null) {
callback('Couldn\'t parse response');
Expand Down
19 changes: 18 additions & 1 deletion test/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ module.exports.multiple = function (assert) {
}, 5000);
}

module.exports.function_url = function (assert) {
module.exports.xml = function (assert) {
osmosis.get(url + '/xml-auto')
.then(function (context, data) {
assert.equal(context.get('link').text(), 'http://example.com');
Expand All @@ -160,6 +160,23 @@ module.exports.function_url = function (assert) {
});
};

module.exports.error_xml_parse = function (assert) {
var tries = 4;

osmosis.get(url + '/get')
.config('parse_as', 'xml')
.config('tries', tries)
.error(function (msg) {
if (msg.indexOf('parsing error') > -1) {
tries--;
}
})
.done(function () {
assert.strictEqual(tries, 0);
assert.done();
});
};

module.exports.absentQueryString = function (assert) {
var found = false;
osmosis.get(url + '/test-query-string')
Expand Down

0 comments on commit 9779590

Please sign in to comment.