Skip to content
This repository was archived by the owner on Aug 25, 2023. It is now read-only.

Commit 45cc9f3

Browse files
committed
Better error handling
1 parent 89ef958 commit 45cc9f3

File tree

5 files changed

+74
-15
lines changed

5 files changed

+74
-15
lines changed

docs/index.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ Nodeogram is released under the GNU General Public License 3.0.
6161
6262
.. _user-documentation:
6363
.. toctree::
64-
:maxdepth: 3
65-
:caption: User Documentation
66-
:glob:
64+
:maxdepth: 3
65+
:caption: User Documentation
66+
:glob:
6767

68-
quickstart
68+
style
69+
quickstart
6970

7071
.. _objects-documentation:
7172
.. toctree::

docs/objects/Bot.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ This object represents a Telegram bot.
1414
:returns: A Bot object
1515

1616
Creates a new Bot object and fetches basic information about it (aysncronous call to Telegram's ``getMe`` method).
17+
1718
Valid ``options`` are:
1819

1920
.. code-block:: javascript
2021
2122
{
22-
profiles_path: __dirname +'/profiles.json'
23+
// File to which profiles should be saved
24+
profiles_path: __dirname +'/profiles.json',
25+
// Whether or not users should be saved
26+
saveUsers: true;
27+
// Whether or not chats should be saved
28+
saveChats: true
2329
}
2430

2531
.. _init:

docs/quickstart.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,12 @@ correct id.
310310
// Inline message
311311
if (query.message) query.message.editText(`You have chosen ${query.data}!`, true)
312312
313+
===================
314+
Profiles management
315+
===================
316+
317+
Nodeogram provides a simple way for you and your bot to keep track of the users and chats that are using your services by
318+
its profile management interface. If a bot is instantiated with ``profiles_path`` in its options, it will start saving
319+
basic information about any user and chat he will become aware of.
320+
321+
The ``broadcast()`` function will enable you to easily access that data and interact with your users.

docs/style.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Design & Best Practices
2+
=======================
3+
4+
==============
5+
Library design
6+
==============
7+
8+
********
9+
Promises
10+
********
11+
12+
The whole Nodeogram library is promise-based. This means that it takes advantage of the relatively new
13+
`ES6 native implementation <https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Promise>`_ of
14+
promises made available in NodeJS starting from release ~0.12.14. Most of the functions provided by the library will
15+
return promises in order to enable you to escape the `callback hell <http://callbackhell.com/>`_.
16+
17+
***************
18+
Handling errors
19+
***************
20+
21+
Nodeogram does not introduce intrusive error handling and as a result you should pay attention and always profile a
22+
``catch`` function.
23+
24+
Nodeogram's functions can fail at any time - querying the API, attempting to send a message to a chat the
25+
bot was kicked out of, etc - and not handling errors properly might result in unexpected behaviours or worse - getting
26+
your bot or IP address tempbanned from Telegram's server could really ruin your day.
27+
28+
==============
29+
Best practices
30+
==============
31+
32+
***************************
33+
Don't feed initialized bots
34+
***************************
35+
36+
Nodeogram's :doc:`objects/Bot` object is intended to be immutable once it's initialized. Making changes to its attributes after
37+
calling the ``init()`` function may produce unexpected behaviours in other objects that rely on them (most notably,
38+
pretty much all of the library objects).

lib/Bot.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ function Bot(token, options) {
2424
this.messageHandlers = [];
2525
this.me = {};
2626
this.emitter = new events.EventEmitter();
27+
this.saveUsers = true;
28+
this.saveChats = true;
2729
extend(this, options);
2830

2931
this.handleAPIError = (err) => {
@@ -49,10 +51,12 @@ function Bot(token, options) {
4951
return this.post(path, args)
5052
.end()
5153
.then((res) => {
52-
if (!res.body.ok) throw "Telegram API response was not OK";
54+
if (!res.body.ok) throw new Error("Telegram API response was not OK");
5355
return res;
5456
}).catch((err) => {
55-
this.handleAPIError(err);
57+
if (err.response.error.status == 403 || err.response.error.status == 400) {
58+
throw new Error(JSON.parse(err.response.error.text).description);
59+
} else this.handleAPIError(err);
5660
})
5761
};
5862

@@ -68,7 +72,9 @@ function Bot(token, options) {
6872
if (!res.body.ok) throw "Telegram API response was not OK";
6973
return res;
7074
}).catch((err) => {
71-
this.handleAPIError(err);
75+
if (err.response.error.status == 403 || err.response.error.status == 400) {
76+
throw new Error(JSON.parse(err.response.error.text).description);
77+
} else throw err;
7278
})
7379
};
7480

@@ -418,21 +424,21 @@ function Bot(token, options) {
418424

419425
this.handleProfiles = (message) => {
420426
var edited = false;
421-
if (!profiles.users[message.from.id]) {
427+
if (!profiles.users[message.from.id] && this.saveUsers) {
422428
profiles.users[message.from.id] = message.from;
423429
profiles.users[message.from.id].bot = undefined;
424430
edited = true;
425431
}
426-
if (!profiles.chats[message.chat.id]) {
432+
if (!profiles.chats[message.chat.id] && this.saveChats) {
427433
profiles.chats[message.chat.id] = message.chat;
428434
profiles.chats[message.chat.id].bot = undefined;
429435
edited = true;
430436
}
431-
if (edited) fs.writeFile(this.profiles_path, JSON.stringify(profiles), (err) => {console.log(err)})
437+
if (edited) fs.writeFile(this.profiles_path, JSON.stringify(profiles), (err) => {if (err) console.log(err)})
432438
};
433439

434440
this.broadcast = (filter, callback) => {
435-
if (!this.profiles_path) throw "Profiles path is not defined";
441+
if (!this.profiles_path) throw new Error("Profiles path is not defined");
436442
if (profiles.chats) {
437443
for (var chat in profiles.chats) {
438444
if (profiles.chats.hasOwnProperty(chat)) {
@@ -447,7 +453,7 @@ function Bot(token, options) {
447453
};
448454

449455
this.init = () => {
450-
if (!this.token) throw "Invalid token!";
456+
if (!this.token) throw new Error("Invalid token!");
451457
this.messageHandlers = [
452458
this.handleCommands,
453459
this.handleMessage
@@ -469,8 +475,7 @@ function Bot(token, options) {
469475

470476
this.call('getMe', {}).then((res) => {
471477
if (res) this.me = res.body.result;
472-
else throw "Unable to retrieve informations about the bot!"
473-
}).catch(err => this.handleAPIError(err));
478+
}).catch(err => console.log(err));
474479

475480
// For more complex event handling use this.emitter
476481
this.on = (event, listener) => {

0 commit comments

Comments
 (0)