Commit 74ddfcb9 authored by yPhil's avatar yPhil
Browse files

New & improved server errors handling

parent 3800c2a0
Pipeline #399293 failed with stage
in 48 seconds
......@@ -215,13 +215,15 @@ PTL.feed = {
PTL.util.say(PTL.tr('Problem reading feed [%1] Error type [%2]', feedUrl, error), 'error');
}).done(function(data) {
console.log('data: %s (%s)', JSON.stringify(data));
$feedLink.text(data.feedTitle || feedUrl)
.attr('href', data.feedLink)
.attr('title', (data.feedTitle || PTL.tr('Untitled')) + ' (' + feedUrl + ')');
if (data.error || (data.feedItems && data.feedItems.length == 0)) {
var message = (data.error && data.error.code) ? data.error.code : PTL.tr('Empty feed');
var message = (data.error) ? data.error : PTL.tr('Empty feed');
PTL.util.say(PTL.tr('Problem reading feed [%1] Error type [%2]', feedUrl, message), 'warning');
......
......@@ -38,8 +38,8 @@ PTL.i18n = {
PTL.i18n.translations = {
"Pétrolette start" : { fr: "Démarrage Pétrolette", ja: "Pétroletteが起動する", es: "Pétrolette iniciando" },
"Pétrolette start finished" : { fr: "Démarrage Pétrolette terminé", ja: "ペトロレットの初期化は正常に終了しました", es: "La inicialización de Pétrolette terminó" },
"Pétrolette start" : { fr: "Démarrage de Pétrolette", ja: "Pétroletteが起動する", es: "Pétrolette iniciando" },
"Pétrolette start finished" : { fr: "Démarrage de Pétrolette terminé", ja: "ペトロレットの初期化は正常に終了しました", es: "La inicialización de Pétrolette terminó" },
"OK": { fr: "OK", ja: "入る", es: "OK" },
"Cancel": { fr: "Annuler", ja: "取り消す", es: "Cancelar" },
......
......@@ -8,7 +8,7 @@ var PTL = (function() {
language: 'en',
start : function() {
PTL.util.say(PTL.tr('Pétrolette starting up OK'), 'success');
PTL.util.say(PTL.tr('Pétrolette start'), 'success');
var $sideMenu = $('nav#sideMenu'),
$overlay = $('#overlay'),
......
......@@ -15,24 +15,6 @@ function maybeDecompress (res, encoding) {
return decompress ? res.pipe(decompress) : res;
}
// function maybeTranslate (res, charset) {
// var iconv;
// // Use iconv if its not utf8 already.
// if (!iconv && charset && !/utf-*8/i.test(charset)) {
// try {
// iconv = new Iconv(charset, 'utf-8');
// console.error('Converting from charset %s to utf-8', charset);
// iconv.on('error', done);
// // If we're using iconv, stream will be the output of iconv
// // otherwise it will remain the output of request
// res = res.pipe(iconv);
// } catch(err) {
// res.emit('error', err);
// }
// }
// return res;
// }
function maybeTranslate (res, charset) {
var iconvStream;
// Decode using iconv-lite if its not utf8 already.
......@@ -51,7 +33,6 @@ function maybeTranslate (res, charset) {
return res;
}
function getParams(str) {
var params = str.split(';').reduce(function (params, param) {
var parts = param.split('=').map(function (part) { return part.trim(); });
......@@ -63,58 +44,9 @@ function getParams(str) {
return params;
}
// function getFeed (urlfeed, callback) {
// var req = request(urlfeed, {timeout: 10000, pool: false});
// req.setMaxListeners(50);
// req.setHeader('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');
// req.setHeader('accept', 'text/html,application/xhtml+xml');
// var feedparser = new FeedParser ();
// var feedItems = [];
// req
// .on ('error', function (err) {
// console.error('AAAh: %s (%s)');
// callback(err);
// })
// .on ('response', function (res) {
// if (res.statusCode != 200) return this.emit('error', new Error('Bad status code'));
// var encoding = res.headers['content-encoding'] || 'identity',
// charset = getParams(res.headers['content-type'] || '').charset;
// res = maybeDecompress(res, encoding);
// res = maybeTranslate(res, charset);
// res.pipe (feedparser);
// });
// feedparser
// .on ('readable', function () {
// try {
// var item = this.read ();
// if (item !== null) feedItems.push (item);
// }
// catch (err) {
// console.error('ERR (%s)', err.message);
// }
// })
// .on ('error', function (err) {
// var meta = this.meta;
// console.error('Error reading feed [%s] (%s)', err.message, urlfeed);
// callback (err.message);
// })
// .on ('end', function () {
// var meta = this.meta;
// callback ('Feed OK', feedItems, meta.title, meta.link);
// return;
// });
// }
function done(err) {
if (err) {
console.log(err, err.stack);
console.error('err: %s Stack %s', err, err.stack);
return;
}
}
......@@ -130,27 +62,31 @@ function getFeed (feedUrl, callback) {
feedparser.on('end', done);
feedparser.on('readable', function() {
try {
var item = this.read ();
var item = this.read();
if (item !== null) feedItems.push (item);
}
catch (err) {
console.error('ERR (%s)', err.message);
console.error('## ERR (%s)', err.message);
}
}).on ('end', function () {
var meta = this.meta;
callback ('Feed OK', feedItems, meta.title, meta.link);
return callback (null, feedItems, meta.title, meta.link);
return;
});
// Handle our response and pipe it to feedparser
if (res.status != 200) throw new Error('Bad status code');
if (res.status != 200) {
console.error('ERR: %s (%s)', JSON.stringify(res.status));
// throw new Error('Bad status code');
return callback(res.status);
}
var charset = getParams(res.headers.get('content-type') || '').charset;
var responseStream = res.body;
responseStream = maybeTranslate(responseStream, charset);
// And boom goes the dynamite
responseStream.pipe(feedparser);
}).catch(done);
}).catch((err) => {
return callback(err);
});
}
Supports Markdown
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