2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-09-01 06:45:41 +00:00

Handle request timeout. Catch errors while parsing JSON

This commit is contained in:
Yago
2016-01-01 21:47:12 +01:00
parent 83b5654a16
commit b1d4e6b416

View File

@@ -65,11 +65,19 @@ TelegramBotPolling.prototype._getUpdates = function () {
})
};
debug('polling with options: %j', opts);
return requestPromise(opts).cancellable().then(function (resp) {
return requestPromise(opts)
.cancellable()
.timeout((10 + this.timeout) * 1000)
.then(function (resp) {
if (resp[0].statusCode !== 200) {
throw new Error(resp[0].statusCode+' '+resp[0].body);
}
var data = JSON.parse(resp[0].body);
var data;
try {
data = JSON.parse(resp[0].body);
} catch (err) {
throw new Error('Error parsing Telegram response: %s', resp[0].body);
}
if (data.ok) {
return data.result;
} else {