Skip to content

Commit

Permalink
src/polling: Emit 'error' instead
Browse files Browse the repository at this point in the history
  • Loading branch information
GochoMugo committed Dec 5, 2017
1 parent 80bf91b commit e94f41b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ that emits the following events:
1. `pre_checkout_query`: Received a new incoming pre-checkout query
1. `polling_error`: Error occurred during polling. See [polling errors](#polling-errors).
1. `webhook_error`: Error occurred handling a webhook request. See [webhook errors](#webhook-errors).
1. `error`: Unexpected error occurred, usually fatal!

**Tip:** Its much better to listen a specific event rather than on
`message` in order to stay safe from the content.
Expand Down
20 changes: 17 additions & 3 deletions src/telegramPolling.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const errors = require('./errors');
const debug = require('debug')('node-telegram-bot-api');
const deprecate = require('depd')('node-telegram-bot-api');
const ANOTHER_WEB_HOOK_USED = 409;
Expand Down Expand Up @@ -105,13 +106,24 @@ class TelegramBotPolling {
updates.forEach(update => {
this.options.params.offset = update.update_id + 1;
debug('updated offset: %s', this.options.params.offset);
this.bot.processUpdate(update);
try {
this.bot.processUpdate(update);
} catch (err) {
err._processing = true;
throw err;
}
});
return null;
})
.catch(err => {
debug('polling error: %s', err.message);
if (!err._processing) {
return this._error(err);
}
delete err._processing;
/*
* An error occured while processing the items,
* i.e. in `this.bot.processUpdate()` above.
* We need to mark the already-processed items
* to avoid fetching them again once the application
* is restarted, or moves to next polling interval
Expand All @@ -135,15 +147,17 @@ class TelegramBotPolling {
* We have to log this to stderr to ensure devops
* understands that they may receive already-processed items
* on app restart.
* We simply can not rescue this situation, emit "error"
* event, with the hope that the application exits.
*/
/* eslint-disable no-console */
const bugUrl = 'https://github.com/yagop/node-telegram-bot-api/issues/36#issuecomment-268532067';
console.error('error: Internal handling of The Offset Infinite Loop failed');
console.error(`error: This was due to error '${requestErr}'`);
console.error(`error: Due to error '${requestErr}'`);
console.error('error: You may receive already-processed updates on app restart');
console.error(`error: Please see ${bugUrl} for more information`);
/* eslint-enable no-console */
throw err;
return this.bot.emit('error', new errors.FatalError(err));
});
})
.finally(() => {
Expand Down

0 comments on commit e94f41b

Please sign in to comment.