LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.58k stars 1.37k forks source link

Script crash not #145

Closed makovez closed 7 years ago

makovez commented 7 years ago

Can I run my script without crash? Even if there is an error going to the next line?

Lonami commented 7 years ago

Need more information.

makovez commented 7 years ago

Is there a way to keep telethon crashing in error? So if there is an error do not crash but continue

Lonami commented 7 years ago

But what crash are you talking about? No library out there will keep running if it has crashed. You have to except that yourself.

makovez commented 7 years ago

Oh ok, because there are some framework that keep their crashing in errors

2017-06-27 10:35 GMT+02:00 Lonami notifications@github.com:

But what crash are you talking about? No library out there will keep running if it has crashed. You have to except that yourself.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/LonamiWebs/Telethon/issues/145#issuecomment-311291490, or mute the thread https://github.com/notifications/unsubscribe-auth/AUsI850M_yMTmqfvQ50FCJ9LEp1QfUZfks5sIL7CgaJpZM4OF18N .

Lonami commented 7 years ago

because there are some framework that keep their crashing in errors

But what do you mean by "crashing in errors"? And what crash are you talking about with Telethon?

makovez commented 7 years ago

Im talking about error in calling method... for example in ImportChatInviteRequest an hash invalid. I want not to stop in these types of errors

Lonami commented 7 years ago

I want not to stop in these types of errors

Well, if a hash is invalid, then you just have to pass the right hash. It will never work if you pass the invalid one. And simply ignoring errors, as you want to do, is definitely not an option. It's good to have errors, and to know what happened. If you get invalid hash, simply surround your call with try/except, as I said before.

makovez commented 7 years ago

I have a class with methods I need. Should I put a try except in every method? Is there no other way?

ps. I need this because i have to control this userbot from telegram chat for example ("/join hash") if i don't put a try except, telethon will crash and i have to go to my vps and restart it

Lonami commented 7 years ago

Should I put a try except in every method?

Not, not every method, just those working with the hash, which may crash.

Is there no other way?

No, not currently, though it's planned to add custom handlers. I just don't know yet how to implement it.

makovez commented 7 years ago

Not, not every method, just those working with the hash, which may crash.

The hash invalid was an example there are so many possible errors in other methods as well. i mean could i try except all class? without put the try except in every method? No, not currently, though it's planned to add custom handlers. I just don't know yet how to implement it.

That would be nice

Lonami commented 7 years ago

Well if you want to blindly ignore all errors (which is strongly not advised), see this example. Assume you have this class:

class A:
    def a(self):
        print('Hello') 
        raise ValueError()
        print('world')

You can write a method and use it like this:

def ignore_errors(clazz, method_name):
    original = getattr(clazz, method_name)
    def new_method(*args, **kwargs):
        try:
            return original(*args, **kwargs)
        except:
            pass
    setattr(clazz, method_name, new_method)

ignore_errors(A, 'a')

So in your case you'd copy the ignore_errors method and then call ignore_errors(TelegramClient, 'invoke'), and all errors will be ignored from now on. Clearly a bad solution. If you know what errors you want to ignore you could slightly improve it:

def ignore_errors(clazz, error_type, method_name):
    original = getattr(clazz, method_name)
    def new_method(*args, **kwargs):
        try:
            return original(*args, **kwargs)
        except error_type:
            pass
    setattr(clazz, method_name, new_method)

ignore_errors(A, ValueError, 'a')
makovez commented 7 years ago

Okk. It works thanks for everything

makovez commented 7 years ago

There is a problem. Telethon resumes from the last error so just one method goes in error the next ones will also be in error

Lonami commented 7 years ago

Telethon resumes from the last error

It shouldn't. What version are you using?

makovez commented 7 years ago

Telethon resumes from the last error

maybe not, i was wrong but after the first error he dont execute the other method

<class 'Exception'> INVITE_HASH_INVALID (code 400)
<class 'Exception'> 'NoneType' object is not iterable
<class 'Exception'> 'NoneType' object is not iterable
<class 'Exception'> 'NoneType' object is not iterable
Lonami commented 7 years ago

<class 'Exception'> 'NoneType' object is not iterable

I presume you don't know the line where this occurs? Like full traceback.

makovez commented 7 years ago

I presume you don't know the line where this occurs? Like full traceback.

If i set the full traceback it will crash in the first error

Lonami commented 7 years ago

Call traceback.print_exc instead?

makovez commented 7 years ago

ok Error found