Telegram.Bot: Error Handling

We have some errors result on SendTextMessage which make throw error exceptions and make slow processes. Is there any solution we safe receive these errors. https://core.telegram.org/method/messages.sendMessage#return-errors

Exception Message: [Error]: PEER_ID_INVALID

We have 403 FORBIDDEN exception when we SendTextMessage to user which blocked the bot. https://core.telegram.org/api/errors

Exception Message: Response status code does not indicate success: 403 (Forbidden).

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (10 by maintainers)

Most upvoted comments

I think that the implementation without throwing exceptions would result in a silent fail for most users. As this is undesirable, I am against changing to condition checking instead of exception handling.

It could be implemented in a manner where the two can gracefully live together, by providing a TryXxx method for every method that returns Task<ResultMessage>, and then reusing that internally.

On the other hand, it would be a good idea to include the error description in the ApiException class.

@amastaneh i think we should indeed throw exeptions. if you really want conditions, just catch those by yourself. Making it conditions would, as @mhverbakel said, result in a silent fail for those user who arent capable of bulding themselves a workaround. Don’t you think it would be better to leave them able to determine the cause of whats happening than you not having to code a single try-catch-loop? if youre sending mass messages, just do it like for example while(condition) { try { //send Message } catch { //handle Exception } } Since I myself used this library even back since I was a little noob, I prefer simplicity before logical correctness.

Exception Handling is a heavy and expensive operation as far as performance is concerned [ref]

We need receive these errors normally (not with an exception) and make decision based on these received messages. Anyway you can find out more respectful documents about error handling with Condition Checking is so better than Exception Handling

I think SendTextMessage need an output from ResultMessage class like

public class ResultMessage
{
    bool IsOk {get; set;}
    Message? Result {get; set;}
    Error? Error {get; set;}
}

public class Error
{
    int Code {get; set;}
    string Description {get; set;}
}