Commit de2881d5 authored by yPhil's avatar yPhil
Browse files

Now catching fetch AND fp errors

parent 05e128b9
Pipeline #417373 passed with stage
in 36 seconds
......@@ -530,21 +530,6 @@ PTL.feed = {
}
// console.log('lastItem: %s', feedLastItem, feedUrl);
if (feedIconHash) {
$favIcon.attr('src', '/favicons/' + feedIconHash + '.favicon');
} else {
await PTL.feed.fetchIcon(feedHost).then((iconhash) => {
if (iconhash) {
$favIcon.attr('src', '/favicons/' + iconhash + '.favicon');
$dataStore.data('iconhash', iconhash);
PTL.tab.saveTabs();
}
}).catch((_error) => {
console.log('Fav error: %s (%s)', feedIconHash, feedUrl);
$favIcon.addClass('icon-rss');
});
}
if ($dataStore.data('status') == 'on') {
......@@ -568,6 +553,23 @@ PTL.feed = {
$badge.fadeOut('slow');
}
if (feedIconHash) {
$favIcon.attr('src', '/favicons/' + feedIconHash + '.favicon');
} else {
await PTL.feed.fetchIcon(feedHost).then((iconhash) => {
if (iconhash) {
$favIcon.attr('src', '/favicons/' + iconhash + '.favicon');
$dataStore.data('iconhash', iconhash);
PTL.tab.saveTabs();
}
}).catch((_error) => {
console.log('Fav error: %s (%s)', feedIconHash, feedUrl);
$favIcon.addClass('icon-rss');
});
}
} catch (error) {
console.log('Catched error: %s (%s)', JSON.stringify(error));
......
......@@ -40,44 +40,50 @@ function handleErrors(response) {
function getFeed (feedUrl, lastItem, callback) {
try {
fetch(feedUrl, {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36',
'accept': 'text/html,application/xhtml+xml',
redirect: 'follow'
}).then(function (res) {
if (res.status !== 200) {
new Error('Bad status code');
callback({error:'error', errno:res.status, message:'error.message'});
}
var feedparser = new FeedParser();
var feedItems = [];
var charset = getParams(res.headers.get('content-type') || '').charset;
var responseStream = res.body;
responseStream = maybeTranslate(responseStream, charset);
responseStream.pipe(feedparser);
return new Promise((resolve, reject) => {
feedparser.on('error', function(error) {
console.error('## feedParserErr: %s (%s)', error.message, feedUrl);
reject('woopsie');
return callback({error:error, errno:res.status, message:error.message});
}).on('readable', function() {
try {
let item;
while ((item = this.read())) {
if (item !== null) {
feedItems.push(item);
}
}
}
catch (err) {
console.error('## feedParserCatchErr: %s (%s)', err, feedUrl);
}
}).on ('end', function () {
const options = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36',
'accept': 'text/html,application/xhtml+xml',
'redirect': 'follow'
}
var req = fetch(feedUrl);
var feedparser = new FeedParser(options);
var feedItems = [];
req.then(function(res) {
if (res.status !== 200) {
console.error('## fetchErr: %s (%s)', feedUrl);
// throw new Error('Bad status code');
callback(res.status);
}
else {
// The response `body` -- res.body -- is a stream
res.body.pipe(feedparser);
}
}, function(error) {
console.error('## fetchErrOtherErr: %s (%s)', error, feedUrl);
callback(error);
}).catch(e => {return false;});
feedparser.on('error', function (error) {
console.error('## XfeedparserErr: %s (%s)', error, feedUrl);
// always handle errors
callback(error);
});
feedparser.on('readable', function() {
try {
let item;
while ((item = this.read())) {
if (item !== null) {
feedItems.push(item);
}
}
}
catch (err) {
console.error('## feedParserCatchErr: %s (%s)', err, feedUrl);
}
}).on ('end', function () {
var newLastItem;
var totalNewItems;
......@@ -98,19 +104,79 @@ function getFeed (feedUrl, lastItem, callback) {
if (totalNewItems == undefined) totalNewItems = i;
var meta = this.meta;
resolve();
// console.error('### Return i:%s, lastItem: [%s], totalNewItems: %d thereArenewItems: [%s]', i, lastItem, totalNewItems);
return callback(null, feedItems, meta.title || 'Untitled', meta.link || feedUrl, newLastItem, totalNewItems);
});
});
// fetch(feedUrl, {
// 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36',
// 'accept': 'text/html,application/xhtml+xml',
// redirect: 'follow'
// }).then(function (res) {
}).catch((error) => {
console.error('Whooaps: %s (%s)', error.message, error.errno);
return callback(error);
});
// if (!res) console.error('## No res: %s (%s)');
// console.error('## res: %s (%s)', JSON.stringify(res), feedUrl);
// if (res.status !== 200) {
// new Error('Bad status code');
// callback({error:'error', errno:res.status, message:'error.message'});
// }
// var feedparser = new FeedParser();
// var feedItems = [];
// var charset = getParams(res.headers.get('content-type') || '').charset;
// var responseStream = res.body;
// responseStream = maybeTranslate(responseStream, charset);
// responseStream.pipe(feedparser);
// return new Promise((resolve, reject) => {
// feedparser.on('error', function(error) {
// console.error('## feedParserErr: %s (%s)', error.message, feedUrl);
// reject('woopsie');
// return callback({error:error, errno:res.status, message:error.message});
// }).on('readable', function() {
// try {
// let item;
// while ((item = this.read())) {
// if (item !== null) {
// feedItems.push(item);
// }
// }
// }
// catch (err) {
// console.error('## feedParserCatchErr: %s (%s)', err, feedUrl);
// }
// }).on ('end', function () {
} catch (err) {
console.error('Whoops: %s (%s)');
}
// var newLastItem;
// var totalNewItems;
// var i = 0;
// feedItems.forEach(countItems);
// function countItems(item) {
// i++;
// // console.error('item: %s', item.link);
// if (newLastItem == undefined) newLastItem = item.link;
// if (item.link == lastItem) {
// totalNewItems = i - 1;
// console.error('Wopop: %s [%s] (%s)', item.link, totalNewItems, i);
// }
// }
// if (totalNewItems == undefined) totalNewItems = i;
// var meta = this.meta;
// resolve();
// // console.error('### Return i:%s, lastItem: [%s], totalNewItems: %d thereArenewItems: [%s]', i, lastItem, totalNewItems);
// return callback(null, feedItems, meta.title || 'Untitled', meta.link || feedUrl, newLastItem, totalNewItems);
// });
// });
// }).catch((error) => {
// console.error('Whooaps: %s (%s)', error.message, error.errno);
// return callback(error);
// });
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment