-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move exception parsing to extension package #14
Comments
I suggest using |
Honestly I don't like this approach... try { bot.SendMsg() }
catch (ParameterException ex) {
foo();
}
catch (ChatBlockedException ex) {
bar();
} instead of try { bot.SendMsg() }
catch (ApiRequestException e) {
switch (parser.Parse(e)) {
case ParameterException:
foo();
case ChatBlockedException:
bar();
}
} from a developer perspective. |
@matteocontrini The problem is not the library size. If exceptions were a part of Bot API and were properly documented then it wouldn't be a problem. But the thing is that they are changed all the time by telegram devs, they're not documented so we have to hunt them down every time therefore we can't guarantee anything stable and it's just complicate things. I understand that the proposed solution is not ideal. |
@matteocontrini is right and I might have a solution for it. Interface for exception parser in client package: // in package Telegram.Bot
interface IExceptionParser { ApiRequestException Parse(JObject response); } User can optionally register an exception parser // using Telegram.Bot.Extensions.Exceptions
var client = new TelegramBotClient(token) {
ExceptionParser = new ExceptionParser()
} And code looks like normal try { bot.SendMsg() }
catch (ParameterException ex) {
foo();
}
catch (ChatBlockedException ex) {
bar();
} |
Looks good. Also, I like poulads approach. |
All right. it seems majority is OK with that. I create a repo for it. |
Furthermore, we can run exception tests periodically (might be daily) using Travis-CI jobs to stay alert when tg changes some of its API behavior. We might want to skip the test cases that require user interaction. |
It's better to be verbose in documenting the exception classes because it isn't covered in bot API. A hierarchical representation of classes would be nice as well (Sandcastle maybe?). |
Proposed changes to Telegram.Bot library are in TelegramBots/Telegram.Bot#694 TelegramBots/Telegram.Bot#695. var exceptionTypeInfo = exceptions.FirstOrDefault(
typeInfo => typeInfo.ErrorCode == apiResponse.ErrorCode
);
return Activator.CreateInstance(
exceptionTypeInfo.exceptionType,
apiResponse.Description
.Replace(exceptionTypeInfo.ErrorCodeDescription, string.Empty),
apiResponse.Parameters
) as ApiRequestException;
); |
I guess this extension point might lead to problems: Here are hardcoded HTTP Errors, that we expect parser will handle: But if we accept external parser, it should provide it's own HTTP Errors list, and this check might not have sense for external exception handler: And this raise another question - should external parser only extend existing exceptions, or deliver all range of exceptions? |
@karb0f0s I think it's okay for exception parser to return any exception type. It seems that it will need to return not only |
Suggestion: move exception parsing functionality to the extension package.
The Telegram API error messages are not part of the API, so they could change at any moment. That is why we cannot guaranty that code based on exceptions would work in changing circumstances. The library should throw only basic exceptions based on API response codes, such as
ApiException
,ForbiddenException
,BadRequestException
for general error codes, such as400
, '403,
429and such. To allow users handle different error codes we could provide separate package
Telegram.Bot.Exceptions`, that will return specific error code (object) that will help users handle different types of errors. This change would allow update only this package to keep up with API changes and keep core library functionality untouched.This change would mean that we will mark all minor exceptions as obsolete, and no more exception minor exceptions would be added to the library.
The text was updated successfully, but these errors were encountered: