mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-31 06:16:07 +00:00
src: Add proper error handling (#283)
Feature: Please see `doc/usage.md` for more information on error-handling.
This commit is contained in:
@@ -27,6 +27,7 @@ const pollingPort = portindex++;
|
||||
const webHookPort = portindex++;
|
||||
const pollingPort2 = portindex++;
|
||||
const webHookPort2 = portindex++;
|
||||
const badTgServerPort = portindex++;
|
||||
const staticUrl = `http://127.0.0.1:${staticPort}`;
|
||||
const key = `${__dirname}/../examples/key.pem`;
|
||||
const cert = `${__dirname}/../examples/crt.pem`;
|
||||
@@ -39,6 +40,8 @@ before(function beforeAll() {
|
||||
return utils.startMockServer(pollingPort)
|
||||
.then(() => {
|
||||
return utils.startMockServer(pollingPort2);
|
||||
}).then(() => {
|
||||
return utils.startMockServer(badTgServerPort, { bad: true });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -125,17 +128,33 @@ describe('TelegramBot', function telegramSuite() {
|
||||
return done();
|
||||
});
|
||||
});
|
||||
it('(polling) emits "polling_error" if error occurs during polling', function test(done) {
|
||||
const myBot = new TelegramBot(12345, { polling: true });
|
||||
myBot.once('polling_error', (error) => {
|
||||
assert.ok(error);
|
||||
assert.equal(error.code, 'ETELEGRAM');
|
||||
return myBot.stopPolling().then(() => { done(); }).catch(done);
|
||||
});
|
||||
});
|
||||
it('(webhook) emits "message" on receiving message', function test(done) {
|
||||
botWebHook.once('message', () => {
|
||||
return done();
|
||||
});
|
||||
utils.sendWebHookMessage(webHookPort2, TOKEN);
|
||||
});
|
||||
it('(webhook) emits "webhook_error" if could not parse webhook request body', function test(done) {
|
||||
botWebHook.once('webhook_error', (error) => {
|
||||
assert.ok(error);
|
||||
assert.equal(error.code, 'EPARSE');
|
||||
return done();
|
||||
});
|
||||
utils.sendWebHookMessage(webHookPort2, TOKEN, { update: 'unparseable!', json: false });
|
||||
});
|
||||
});
|
||||
|
||||
describe('WebHook', function webHookSuite() {
|
||||
it('returns 200 OK for health endpoint', function test(done) {
|
||||
utils.sendWebHookRequest(webHookPort2, '/healthz', { json: false }).then(resp => {
|
||||
utils.sendWebHookRequest(webHookPort2, '/healthz').then(resp => {
|
||||
assert.equal(resp, 'OK');
|
||||
return done();
|
||||
});
|
||||
@@ -186,6 +205,58 @@ describe('TelegramBot', function telegramSuite() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('errors', function errorsSuite() {
|
||||
const botParse = new TelegramBot('useless-token', {
|
||||
baseApiUrl: `http://localhost:${badTgServerPort}`,
|
||||
});
|
||||
it('FatalError is thrown if token is missing', function test() {
|
||||
const myBot = new TelegramBot(null);
|
||||
return myBot.sendMessage(USERID, 'text').catch(error => {
|
||||
// FIX: assert.ok(error instanceof TelegramBot.errors.FatalError);
|
||||
assert.equal(error.code, 'EFATAL');
|
||||
assert.ok(error.message.indexOf('not provided') > -1);
|
||||
});
|
||||
});
|
||||
it('FatalError is thrown if file-type of Buffer could not be determined', function test() {
|
||||
let buffer;
|
||||
try {
|
||||
buffer = Buffer.from('12345');
|
||||
} catch (ex) {
|
||||
buffer = new Buffer('12345');
|
||||
}
|
||||
return bot.sendPhoto(USERID, buffer).catch(error => {
|
||||
// FIX: assert.ok(error instanceof TelegramBot.errors.FatalError);
|
||||
assert.equal(error.code, 'EFATAL');
|
||||
assert.ok(error.message.indexOf('Unsupported') > -1);
|
||||
});
|
||||
});
|
||||
it('FatalError is thrown on network error', function test() {
|
||||
const myBot = new TelegramBot('useless-token', {
|
||||
baseApiUrl: 'http://localhost:23', // are we sure this port is not bound to?
|
||||
});
|
||||
return myBot.getMe().catch(error => {
|
||||
// FIX: assert.ok(error instanceof TelegramBot.errors.FatalError);
|
||||
assert.equal(error.code, 'EFATAL');
|
||||
});
|
||||
});
|
||||
it('ParseError is thrown if response body could not be parsed', function test() {
|
||||
botParse.sendMessage(USERID, 'text').catch(error => {
|
||||
// FIX: assert.ok(error instanceof TelegramBot.errors.ParseError);
|
||||
assert.equal(error.code, 'EPARSE');
|
||||
assert.ok(typeof error.response === 'object');
|
||||
assert.ok(typeof error.response.body === 'string');
|
||||
});
|
||||
});
|
||||
it('TelegramError is thrown if error is from Telegram', function test() {
|
||||
return bot.sendMessage('404', 'text').catch(error => {
|
||||
// FIX: assert.ok(error instanceof TelegramBot.errors.TelegramError);
|
||||
assert.equal(error.code, 'ETELEGRAM');
|
||||
assert.ok(typeof error.response === 'object');
|
||||
assert.ok(typeof error.response.body === 'object');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#startPolling', function initPollingSuite() {
|
||||
it('initiates polling', function test() {
|
||||
return testbot.startPolling().then(() => {
|
||||
@@ -195,6 +266,8 @@ describe('TelegramBot', function telegramSuite() {
|
||||
it('returns error if using webhook', function test() {
|
||||
return botWebHook.startPolling().catch((err) => {
|
||||
// TODO: check for error in a better way
|
||||
// FIX: assert.ok(err instanceof TelegramBot.errors.FatalError);
|
||||
assert.equal(err.code, 'EFATAL');
|
||||
assert.ok(err.message.indexOf('mutually exclusive') !== -1);
|
||||
});
|
||||
});
|
||||
@@ -235,6 +308,8 @@ describe('TelegramBot', function telegramSuite() {
|
||||
it('returns error if using polling', function test() {
|
||||
return botPolling.openWebHook().catch((err) => {
|
||||
// TODO: check for error in a better way
|
||||
// FIX: assert.ok(err instanceof TelegramBot.errors.FatalError);
|
||||
assert.equal(err.code, 'EFATAL');
|
||||
assert.ok(err.message.indexOf('mutually exclusive') !== -1);
|
||||
});
|
||||
});
|
||||
@@ -1020,7 +1095,7 @@ describe('TelegramBot', function telegramSuite() {
|
||||
const photo = `${__dirname}/data/photo.gif`;
|
||||
return tgbot.sendPhoto(USERID, photo).catch(err => {
|
||||
// TODO: check for error in a better way
|
||||
assert.ok(err.response.body.indexOf('Bad Request') !== -1);
|
||||
assert.ok(err.response.body.description.indexOf('Bad Request') !== -1);
|
||||
});
|
||||
});
|
||||
it('should allow stream.path that can not be parsed', function test() {
|
||||
|
Reference in New Issue
Block a user