mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-09-01 23:05:21 +00:00
Handle request timeout. Catch errors while parsing JSON
This commit is contained in:
@@ -65,17 +65,25 @@ TelegramBotPolling.prototype._getUpdates = function () {
|
|||||||
})
|
})
|
||||||
};
|
};
|
||||||
debug('polling with options: %j', opts);
|
debug('polling with options: %j', opts);
|
||||||
return requestPromise(opts).cancellable().then(function (resp) {
|
return requestPromise(opts)
|
||||||
if (resp[0].statusCode !== 200) {
|
.cancellable()
|
||||||
throw new Error(resp[0].statusCode+' '+resp[0].body);
|
.timeout((10 + this.timeout) * 1000)
|
||||||
}
|
.then(function (resp) {
|
||||||
var data = JSON.parse(resp[0].body);
|
if (resp[0].statusCode !== 200) {
|
||||||
if (data.ok) {
|
throw new Error(resp[0].statusCode+' '+resp[0].body);
|
||||||
return data.result;
|
}
|
||||||
} else {
|
var data;
|
||||||
throw new Error(data.error_code+' '+data.description);
|
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 {
|
||||||
|
throw new Error(data.error_code+' '+data.description);
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = TelegramBotPolling;
|
module.exports = TelegramBotPolling;
|
||||||
|
Reference in New Issue
Block a user