Commit 05e128b9 authored by yPhil's avatar yPhil
Browse files

No fetch errors (#bug)

parent c6158b42
......@@ -65,7 +65,10 @@
"eslint:recommended"
],
"rules": {
"semi-style": ["error", "last"],
"semi-style": [
"error",
"last"
],
"quotes": [
"error",
"double",
......
......@@ -13,7 +13,7 @@ PTL.feed = {
const $feedImg = $('<img>')
.attr({
src: '/favicons/' + iconhash + '.favicon',
src: '/static/images/rss.gif',
class: 'favicon',
width: '16px',
height: '16px'
......@@ -412,9 +412,11 @@ PTL.feed = {
},
errorFeed:function(error, feedUrl) {
console.log('pow: %s', feedUrl);
const type = (error.statusText) ? error.statusText : PTL.tr('Unknown error');
const errno = (error.responseJSON.errno) ? error.responseJSON.errno : '0';
const message = error.responseJSON.message;
const errno = (error.responseJSON) ? error.responseJSON.errno : '0';
const message = (error.responseJSON) ? error.responseJSON.message : 'Empty';
const $validateLink = $('<a>')
.attr('href', 'https://validator.w3.org/feed/check.cgi?url=' + feedUrl);
......@@ -527,6 +529,8 @@ PTL.feed = {
.addClass('folded');
}
// console.log('lastItem: %s', feedLastItem, feedUrl);
if (feedIconHash) {
$favIcon.attr('src', '/favicons/' + feedIconHash + '.favicon');
} else {
......@@ -536,38 +540,44 @@ PTL.feed = {
$dataStore.data('iconhash', iconhash);
PTL.tab.saveTabs();
}
}).catch((error) => {
console.log('error: %s (%s)',error);
}).catch((_error) => {
console.log('Fav error: %s (%s)', feedIconHash, feedUrl);
$favIcon.addClass('icon-rss');
});
}
if ($dataStore.data('status') == 'on') {
$feedIcon.removeClass('fold');
$refreshButton.addClass('spin');
// $feedLink.removeClass('danger');
try {
let fetchFeed = await PTL.feed.fetchFeed(feedUrl, feedLastItem);
$feedIcon.removeClass('fold');
$refreshButton.addClass('spin');
let fetchFeed = await PTL.feed.fetchFeed(feedUrl, feedLastItem);
console.log('lastItem: %s (%s)', JSON.stringify(fetchFeed.lastItem), fetchFeed.totalNewItems);
let lastItems = await PTL.feed.lastItems(fetchFeed.feedItems, $dataStore);
$feedBody.html(lastItems[0]);
let lastItems = await PTL.feed.lastItems(fetchFeed.feedItems, $dataStore);
$feedBody.html(lastItems[0]);
$refreshButton.removeClass('spin');
$refreshButton.removeClass('spin');
$dataStore.data('lastitem', fetchFeed.lastItem);
if (fetchFeed.totalNewItems > 0) {
$dataStore.data('lastitem', fetchFeed.lastItem);
PTL.tab.saveTabs();
$badge.text(fetchFeed.totalNewItems).fadeIn('slow');
} else {
$badge.fadeOut('slow');
}
} catch (error) {
console.log('Catched error: %s (%s)', JSON.stringify(error));
$feedBody.empty().append(PTL.feed.errorFeed(error, feedUrl));
$feedBody.css('height', '');
$refreshButton.removeClass('spin');
if (fetchFeed.totalNewItems > 0) {
// let lastItems = await PTL.feed.lastItems(fetchFeed.feedItems, $dataStore);
// $feedBody.prepend(lastItems[0]);
// PTL.db.put(feedUrl, $feedBody.html());
$badge.text(fetchFeed.totalNewItems).fadeIn('slow');
} else {
// $dataStore.data('lastitem', 'none');
$badge.fadeOut('slow');
}
} else {
$dataStore
......
......@@ -32,6 +32,12 @@ function getParams(str) {
return params;
}
function handleErrors(response) {
console.error('## FetchErr: %s (%s)');
if (!response.ok) throw new Error(response.status);
return response;
}
function getFeed (feedUrl, lastItem, callback) {
try {
......@@ -42,6 +48,11 @@ function getFeed (feedUrl, lastItem, callback) {
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;
......@@ -76,23 +87,26 @@ function getFeed (feedUrl, lastItem, callback) {
function countItems(item) {
i++;
// console.error('item: %s', item.link);
if (newLastItem == undefined) newLastItem = item.link;
if (item.link == lastItem) {
totalNewItems = Number(i - 1);
totalNewItems = i - 1;
console.error('Wopop: %s [%s] (%s)', item.link, totalNewItems, i);
}
}
if (totalNewItems == undefined) totalNewItems = i;
var meta = this.meta;
var thereArenewItems = false;
resolve();
console.error('### Return i:%s, lastItem: [%s], totalNewItems: %d thereArenewItems: [%s]', i, lastItem, totalNewItems, thereArenewItems);
// 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);
callback(error);
return callback(error);
});
} catch (err) {
......
......@@ -36,11 +36,13 @@ router.get('/favicon', function(req, res) {
if (err) console.error('######### err: %s (%s)', err);
// console.error('######### favicon: %s (%s)', url);
if (url) {
console.error('######### url: %s (%s)',url);
// console.error('######### url: %s (%s)',url);
if (!url.startsWith('http')) url = 'http://' + url.substring(url.indexOf("/") + 1);
if (!url.startsWith('http') || !url.startsWith('//')) url = 'http://' + url.substring(url.indexOf("/") + 1);
const hash = crypto.createHash('md5').update(url).digest('hex'),
fileName = hash + '.favicon',
......@@ -74,7 +76,7 @@ router.get('/feed', function(req, res) {
feeder.getFeed(req.query.url, req.query.lastItem, function (error, feedItems, feedTitle, feedLink, lastItem, totalNewItems) {
console.error('### totalNewItems: [%s]', totalNewItems);
// console.error('### totalNewItems: [%s] oL:%s, nL:%s', totalNewItems, req.query.lastItem, lastItem);
if (feedItems && !res.headersSent) {
res.send({
......
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