yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.45k stars 1.53k forks source link

How to catch Errors thrown by request async code?! #798

Open Apollon77 opened 4 years ago

Apollon77 commented 4 years ago

One of our users got an exception like https://github.com/iobroker-community-adapters/ioBroker.telegram/issues/152

When I check your code you simply throw errors in asny methods like https://github.com/yagop/node-telegram-bot-api/blob/master/src/telegram.js#L283 ... it is near to impossible to good catch them :-( What about introducing error events or other way that allows to catch such errors?

Or do I miss something?

kamikazechaser commented 4 years ago

The line you quoted above (L283) is specific for the Telegram API. The Telegram API will always return a status 200 even if your query is bad, naturally you would expect a 4XX, but not with the Telegram API.

Hence, with the request builder, we have to parse the 200 response from Telegram servers, if its a bad query, we throw the error.

Any other error e.g. lost internet connection, proxy errors, e.t.c. can be caught. The user seems to have lost connection hence the error.

Apollon77 commented 4 years ago

Exactly. But how to catch them? A try catch around the api call will not do the job as i experienced. So only way i see Is to use the global „uncaugth exception“ handler which is not the best way ...

kamikazechaser commented 4 years ago

Maybe we could instead throw individual errors which could be from a known object of Telegram errors?

Apollon77 commented 4 years ago

But also these would be not catchable unless you use the global exception handler (yes more easy to detect that way and so better then nothing. Better would be to allow to provide an on-Error callback somewhere or a promise reject if promises are used or an error event if events are used.